PHP rename() | Rename a File or Directory

The PHP rename() function is used when we need to change the name of a file or a directory. For example:

<?php
   $chk = rename("myfile.txt", "yourfile.txt");
   if($chk)
      echo "<p>Name changed successfully!</p>";
   else
      echo "<p>Unable to change the name.</p>";
?>

The output of above PHP example on rename() function is shown in the snapshot given below:

php rename function

That is, after executing the above PHP code, the name of myfile.txt file get changed to yourfile.txt file.

PHP rename() Syntax

The syntax of rename() function in PHP, is:

rename(oldName, newName, context)

The first two (oldName and newName) parameters are required, whereas the last parameter is optional, that is used to specify the context resource while renaming a file or a directory.

PHP Online Test


« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook




Subscribe Us on YouTube