php

How to Rename a File or Directory in PHP Using the rename() Function

The rename() function is an effective function in PHP that allows you to accomplish the file and directory renaming task effortlessly. Whether you want to change the name of a single file in one go, the rename() function is an ideal choice for that.

In this guide, we will discuss the usage and syntax of the rename() function in PHP. 

What is a rename() Function in PHP?

The rename() function is a built-in PHP function used to change the name of the file or directory without changing the content of the file. This function takes the old name and new name of the file as necessary arguments.

Syntax

The format for using the rename() function in PHP is as follows:

rename(old, new, context)

The old and new are the compulsory parameters, the old specifies the file you want to change, new specifies the new name for the file. The context is an optional parameter that determines the context for changing the file name.

The rename() function returns True on the successful execution, else, it returns False. The rename() file returns an error if the new file already exists in the same directory.

How to Use rename() Function in PHP?

Renaming the file through the rename() function is easy, and the following examples will help you in that case.

 How to Rename a File using PHP rename() Function

The following code will rename a file test.php to a file.php:

<?php
rename("test.php", "file.php");
?>

If you rename a file that already exists in the same directory, it will be overwritten.

Example 1 – How to Rename a File using PHP rename() Function

The following example will rename the test.php to test2.php. On successful completion, the if statement will be printed on the console, otherwise, the else statement will be displayed as an output:

<?php
if (rename("C:\\xampp\\htdocs\\test.php","C:\\xampp\\htdocs\\test2.php"))
{
echo " Successfully renamed test.php to test2.php";
}
else {
echo "Error while renaming file";
}
?>

Note: In PHP, when specifying file paths for the rename() function (and other file-related operations), it’s important to use double backslashes (\\) instead of a single slash (\) to ensure that the file path is interpreted correctly, this is because a single backslash is treated as an escape character in PHP.

Example 2 – How to Rename a Directory using PHP rename() Function

To rename a directory using the rename() function, follow the below-given code:

$oldDirName = 'documents';
$newDirName = 'samplefiles';

if (rename($oldDirName, $newDirName)) {
    echo "Directory renamed successfully!";
} else {
    echo "Error while renaming directory.";
}

Bottom Line

PHP’s rename() function is a reliable tool for effortlessly renaming a file or directory while preserving its content. Its simple syntax and error-handling capabilities make it a convenient choice for file management tasks. By leveraging the power of rename(), PHP developers can efficiently organize and modify file or directory names in their projects.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.