Book HomePHP CookbookSearch this book

19.7. Copying or Moving a File

19.7.1. Problem

You want to copy or move a file.

19.7.2. Solution

Use copy( ) to copy a file:

copy($old,$new) or die("couldn't copy $old to $new: $php_errormsg");

Use rename( ) to move a file:

rename($old,$new) or die("couldn't move $old to $new: $php_errormsg");

19.7.3. Discussion

On Unix, rename( ) can't move files across filesystems. To do so, copy the file to the new location and then delete the old file:

if (copy("/tmp/code.c","/usr/local/src/code.c")) {
  unlink("/tmp/code.c");
}

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.

19.7.4. See Also

Documentation on copy( ) at http://www.php.net/copy and rename( ) at http://www.php.net/rename.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.