PHP | rename( ) Function - GeeksforGeeks

29 May, 2018
The rename() function in PHP is an inbuilt function which is used to rename a file or directory. It makes an attempt to change an old name of a file or directory with a new name specified by the user and it may move between directories if necessary.If the new name specified by the user already exists, the rename() function overwrites it. The old name of the file and the new name specified by the user are sent as parameters to the rename() function and it returns True on success and a False on failure.
Syntax:
rename(oldname, newname, context)
Parameters Used:The rename() function in PHP accepts three parameter.
oldname : It is a mandatory parameter which specifies the old name of the file or directory.newname : It is a mandatory parameter which specifies the new name of the file or directory.context : It is an optional parameter which specifies the behavior of the stream .
oldname : It is a mandatory parameter which specifies the old name of the file or directory.
newname : It is a mandatory parameter which specifies the new name of the file or directory.
context : It is an optional parameter which specifies the behavior of the stream .
Return Value:It returns True on success and a False on failure.
Errors And Exception
The rename() function generates a Warning if the newname already exists while renaming a directory.The wrapper used in oldname must match the wrapper used in newname.If the destination filesystem doesn’t permit chown() or chmod() system calls to be made on files, then rename() function may generate Warnings.
The rename() function generates a Warning if the newname already exists while renaming a directory.
The wrapper used in oldname must match the wrapper used in newname.
If the destination filesystem doesn’t permit chown() or chmod() system calls to be made on files, then rename() function may generate Warnings.
Examples:
Input : $old_name = "gfg.txt" ; 
        $new_name = "newgfg.txt" ; 
        rename( $new_name, $old_name) ;

Output : 1

Input : $old_name = "gfg.txt" ;
        $new_name = "newgfg.txt" ; 
        if(file_exists($new_name))
        { 
           echo "Error While Renaming $old_name" ;
        }
        else
        {
           if(rename( $old_name, $new_name))
           { 
           echo "Successfully Renamed $old_name to $new_name" ;
           }
          else
          {
           echo "A File With The Same Name Already Exists" ;
          }
        }

Output : Successfully Renamed gfg.txt to newgfg.txt

Below programs illustrate the rename() function.
Suppose there is a file named “gfg.txt”
Program 1
<?php // Old Name Of The file$old_name = "gfg.txt" ;   // New Name For The File$new_name = "newgfg.txt" ;   // using rename() function to rename the filerename( $old_name, $new_name) ;  ?>
Output:
1
Program 2
<?php // Old Name Of The file$old_name = "gfg.txt" ;    // New Name For The File$new_name = "newgfg.txt" ;    // Checking If File Already Existsif(file_exists($new_name)) {    echo "Error While Renaming $old_name" ; }else {   if(rename( $old_name, $new_name))     {         echo "Successfully Renamed $old_name to $new_name" ;     }     else     {        echo "A File With The Same Name Already Exists" ;     }  }?>
Output:
Successfully Renamed gfg.txt to newgfg.txt
Related Articles:
PHP | copy( ) Function
PHP | unlink() function
Reference:http://php.net/manual/en/function.rename.php
PHP-file-handling
PHP-function
PHP
Web technologies Questions
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
PHP | Converting string to Date and DateTime
How to pass JavaScript variables to PHP ?
Split a comma delimited string into an array in PHP
Remove elements from a JavaScript Array
Installation of Node.js on Linux
How to insert spaces/tabs in text using HTML/CSS?
How to set the default value for an HTML <select> element ?
File uploading in React.js