PHP: Rename or move a file on the server

Home -> Programming -> Web -> PHP

16715 views

From the computer of: qmchenry (338 recipes)
Created: Jul 10, 2006


Add a comment

Add to:
Add to stumbleuponAdd to del.icio.usDigg itAdd to FURL

Working with server-side files with PHP adds a great deal of flexibility to some applications. A common scenario when working with files involves creating a temporary file through uploading or other means and then renaming this file to a permanent location. This recipe describes changing the name of an existing file using the PHP function rename().

The syntax for the PHP rename function is consistent with that of the UNIX mv function, which always makes things more comfortable. The general syntax is:

rename('/path1/old_filename', '/path2/new_filename');


The rename function returns true if the rename was successful, and false otherwise.

Just like with the unlink function which deletes files, the rename function most likely fails because of file permissions. To rename a file, the directory containing it must be writable by the user trying to rename it. The file's permissions (and ownership) are irrelevant.

Subscribe to the Tech-Recipes Newsletter

You can get tips like this delivered in your email every week!

Enter your Email

We will never, ever sell your email address or spam you.





Related recipes:

  PHP: Delete or unlink a file on the server
  PHP: Upload Files to the server
  PHP: Create a File on your Server
  Solve PHP Fatal error: Allowed memory size of 8388608 bytes exhausted (tried...
  Solve PHP error: Cannot modify header information - headers already sent
  PHP: Set or create a simple browser cookie with setcookie
  PHP: determine the directory path of the server root
  PHP: Generate Dynamic Images from Text
  PHP: Password Protect Your pages.
  PHP: Delete or expire a browser cookie

 

Sponsored links

 

Login

Nickname

Password

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.