The copy() function in PHP is used to copy a file from source to target or destination directory. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure.
PHP File handling functions :copy() – used to copy a file. rename() – used to rename a file. unlink() – used to delete a file.
You could use the copy()
function :
// Will copy foo/test.php to bar/test.php
// overwritting it if necessary
copy('foo/test.php', 'bar/test.php');
Quoting a couple of relevant sentences from its manual page :
Makes a copy of the file source to dest.
If the destination file already exists, it will be overwritten.
You could use the rename() function :
rename('foo/test.php', 'bar/test.php');
This however will move the file not copy
copy will do this. Please check the php-manual. Simple Google search should answer your last two questions ;)
Best way to copy all files from one folder to another using PHP
<?php
$src = "/home/www/example.com/source/folders/123456"; // source folder or file
$dest = "/home/www/example.com/test/123456"; // destination folder or file
shell_exec("cp -r $src $dest");
echo "<H2>Copy files completed!</H2>"; //output when done
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With