PHP copying or moving a file
In this article, you will learn how to copy or move a file. PHP provides copy() and rename() functions to copy and move files.
PHP copy()
The function copies a file from a source to a 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 copying 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");
Related Articles
PHP sanitize input for MySQLPHP random quote generator
PHP String Contains
PHP calculate percentage of total
PHP Fix: invalid argument supplied for foreach
Locking files with flock()
How to Pass an Array as URL Parameter in PHP
How to generate pdf in PHP using MySQL and MPDF Library
How to Export MySQL Table data as CSV file in PHP
Read CSV file & Import data into MySQL with PHP
Save an emoji in MySQL using PHP
Google reCAPTCHA v2 PHP example
Fetch data from database in PHP and display in PDF
How to create a PDF from a form using PHP
Form Validation with PHP
Get Visitor Information by IP Address in PHP
PHP7 Password Hashing