Archive for March 6th, 2010

Alternate to php copy, move function

In some cases php developers don’t have permissions to use php copy function or face difficulty to use it. For that there is a simple way to copy a file from one place to other.

To do so get the contents of the file you want to copy, suppose you have a file, “test.php” and want to copy it. i.e

$copy=file_get_contents(“test.php”);

Next open a new file in the location where you want to copy the file. Suppose you want to copy in the directory “data”, then do as:

$fp=fopen(“data/test.php”,”w”);

This will open a new file in “data” directory with “test.php” name. You can use any other name you want. Now write the file contents to newly open file and close the the file source.

fwrite($fp,$copy);
fclose($fp);

This will write all the content stored in $copy variable to the new file. If you are using this method to move a file then at the end just use the unlink(delete) function to delete the original file. Here is a complete code:

<?php
//File to copy
$copy=file_get_contents(“test.php”);
//where to copy and with what name
$fp=fopen(“data/test.php”,”w”);
fwrite($fp, $copy);
fclose($fp);
//If your are moving file
unlink(“test.php”);
?>

Remember you must have the write permission to “data” directory in order to create new file. i.e chmod 777.

Searched By:

move function php (1), php copy alternative (1), php copy file_get_contents (1), sample marketiva soap with visual studio 2010 (1)