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/*from w  ww.  j  a v  a 2 s.  c  om*/
      $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";
      }
?>




















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions