Hudzilla.org - the homepage of Paul Hudson
Contents > Files Wish List | Report Bug | About Me ]

8.3     Moving, copying, and deleting files: rename(), copy(), and unlink()

This is NOT the latest copy of this book; click here for the latest version.

bool rename ( string old_name, string new_name [, resource context])

bool copy ( string source, string dest)

bool unlink ( string filename, resource context)

PHP has simple functions to handle all moving, copying, and deleting files, and quite rightly - they are all very popular things to do, so there is no point making them difficult. If you are using Unix you will know that there is no command for "rename", because renaming a file is essentially the same as moving it, so you use the move (mv) command - it is the same in PHP.

Files are moved using rename(), copied using copy(), and deleted using unlink(). Unlink() might seem like an odd choice of word at first, but Unix systems consider filenames to be "hard links" to the actual files themselves, so to unlink a file is to delete it.

Author's Note: all three functions will operate without further input from you. If you choose to pass an existing file to the second parameter of rename(), it will rename the file in parameter one to the file in parameter two, overwriting the original file. The same applies to copy() - you will overwrite all files without question as long as you have the correct permissions.





<< 8.2.2 fwrite()   8.3.1 Moving files with rename() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is five plus one?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow