PHP Tutorial - PHP rename() Function






Definition

The rename() function renames a file or directory.

Syntax

PHP rename() Function has the following syntax.

rename(oldname,newname,context)

Parameter

ParameterIs RequiredDescription
oldnameRequired.File or directory to be renamed
newnameRequired.New name of the file or directory
contextOptional.Context of the file handle.

Return

This function returns TRUE on success, or FALSE on failure.





Note

The rename() function should be used to move ordinary files, and not files uploaded through a form.

move_uploaded_file() checks to make sure the file has indeed been uploaded before moving it.

Example

renames a file or directory


<?PHP/* w  w  w  . j a  v a 2 s.  c  o m*/
      $filename = "c:/abc/test.txt";
      $filename2 = $filename . '.old';                                                                    
      $result = rename($filename, $filename2);                                                            
      if ($result) {
             print "$filename has been renamed to $filename2.\n";
      } else {
             print "Error: couldn't rename $filename to $filename2!\n";
      }
?>