PHP copying or moving a file
In this article, you will learn how to copying or moving a file. PHP provides copy() and rename() functions to copy and move files.
PHP copy()
The function copies a file from a source to destination folder. It returns TRUE on success and FALSE on failure.
Syntax of copy()
copy($source, $dest)
Here, $source is the path to the source file and $dest is the path to the destination file.
Example
$oldfile = "/tmp/user.txt";
$newfile = "/tmp/approveduser.txt";
copy($oldfile,$newfile) or die("Error on copy $oldfile to $newfile: $php_errormsg");
PHP rename()
The rename() command is most recommended in case we do not need to keep the file in the source folder. So, basically it moves the file to the destination instead of copy it. It returns TRUE on success and FALSE on failure.
Syntax of rename()
rename($source, $dest)
Here, $source is the path to the source file and $dest is the path to the destination file.
Example
$oldfile = "/tmp/user.txt";
$newfile = "/tmp/approveduser.txt";
rename($oldfile,$newfile) or die("Error on move $oldfile to $newfile: $php_errormsg");