Posts tagged ‘php copy function’

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:

php copy alternative (17), alternative to php copy (3), alternative copy function in php (2), php copy vs fwrite (2), php alternative to copy (2), php copy() alternative (1), php copy fwrite (1), php copy alterneti̇ff (1), php alternative zu copy() (1), php alternative copy (1), fwrite alternative php (1), copy function alternative in php (1), alternative to php copy() (1), alternative php copy() (1), alternative of copy in php (1), alternative for copy function in php (1), php non-fopen alternative to copy (1)