Thursday, February 5, 2026
HomeLanguagesPHP | rename( ) Function

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




<?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) ;
  
?>


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 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" ;
     }
  }
?>


Output:

Successfully Renamed gfg.txt to newgfg.txt

Related Articles:

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

RELATED ARTICLES

Most Popular

Dominic
32483 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6859 POSTS0 COMMENTS
Nicole Veronica
11983 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12070 POSTS0 COMMENTS
Shaida Kate Naidoo
6994 POSTS0 COMMENTS
Ted Musemwa
7233 POSTS0 COMMENTS
Thapelo Manthata
6942 POSTS0 COMMENTS
Umr Jansen
6924 POSTS0 COMMENTS