PHP can only copy, rename or move files that are of the same owner with the php script. After making sure of this, you can proceed to:
Copy a file to another place in PHP:
$old = '/tmp/yesterday.txt';
$new = '/tmp/today.txt';
copy($old, $new) or die("Unable to copy $old to $new.");
Rename a file and give it a new name:
$old = '/tmp/today.txt';
$new = '/tmp/tomorrow.txt';
rename($old, $new) or die("Unable to rename $old to $new.");
Move a file to a new place in PHP:
if (copy("/tmp/code.c","/usr/local/src/code.c")) {
unlink("/tmp/code.c");
}
Apparently you must make sure the copying is successful before doing the deletion.
Of course you can always execute a native Linux shell command such as ‘cp’ and ‘mv’ to do the dirty work for you instead of resorting to a PHP function. In this situation, you need the php function exec() to execute an external command.

{ 1 comment… read it below or add one }
Hi, maybe you could help me with a problem i’m having, i need ti rename a file, with a specific name, how can i create a function that let do the following, if the file exist, rename the file with a new name like, document.php, document_2.php, document_3.php, document_4.php. i need to create a function to do that, when it renames a file rename it with the next number.
thank you
{ 1 trackback }