PHP Directories Copying or Moving a File - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Directories Copying or Moving a File - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Monday, July 15, 2019

PHP Directories Copying or Moving a File

PHP Directories 



Copying or Moving a File

Problem

You want to copy or move a file.

Solution

Use copy() to copy a file:

       $old = '/tmp/yesterday.txt';
       $new = '/tmp/today.txt';
       copy($old,$new) or die("couldn't copy $old to $new: $php_errormsg");

Use rename() to move a file:

       $old = '/tmp/today.txt';
       $new = '/tmp/tomorrow.txt';
       rename($old,$new) or die("couldn't move $old to $new: $php_errormsg");

Discussion

If you have multiple files to copy or move, call copy() or rename() in a loop. You can operate only on one file each time you call these functions.

No comments:

Post a Comment

Post Top Ad