PHP | rename( ) Function

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.



  1. oldname : It is a mandatory parameter which specifies the old name of the file or directory.
  2. newname : It is a mandatory parameter which specifies the new name of the file or directory.
  3. 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

  1. The rename() function generates a Warning if the newname already exists while renaming a directory.
  2. The wrapper used in oldname must match the wrapper used in newname.
  3. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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 file
rename( $old_name, $new_name) ;
  
?>

chevron_right


Output:

1

Program 2

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// Old Name Of The file
$old_name = "gfg.txt"
   
// New Name For The File
$new_name = "newgfg.txt"
   
// Checking If File Already Exists
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" ;
     }
  }
?>

chevron_right


Output:

Successfully Renamed gfg.txt to newgfg.txt

Related Articles:

Reference:
http://php.net/manual/en/function.rename.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.