rename()
In PHP, the rename() function is used to rename or move a file or directory. It is a useful function for managing files and directories in your PHP scripts. In
Introduction
In PHP, the rename() function is used to rename or move a file or directory. It is a useful function for managing files and directories in your PHP scripts. In this article, we will cover everything you need to know about the rename() function, including its syntax, parameters, and examples of how it can be used.
Understanding the rename() Function
The rename() function in PHP is used to rename or move a file or directory. It takes two parameters: the current name or path of the file or directory, and the new name or full destination path.
When you use rename(), PHP renames or moves the file or directory to the new location. This can be useful for managing files and directories in your PHP scripts. Note that moving a file across different filesystems may not be supported on all platforms, and the web server process must have read/write permissions for both the source and destination paths.
Syntax of the rename() Function
The syntax of the rename() function is as follows:
rename($old_name, $new_name);Here, $old_name is the current name or path of the file or directory, and $new_name is the new name or full destination path. The function returns true on success and false on failure.
Examples of Using rename()
Let's take a look at some examples of how the rename() function can be used in PHP.
Example 1: Renaming a File
if (rename('example.txt', 'new_example.txt')) {
echo "File renamed successfully.";
} else {
echo "Failed to rename the file.";
}This example renames the example.txt file to new_example.txt.
Example 2: Moving a File
if (rename('example.txt', 'example_directory/example.txt')) {
echo "File moved successfully.";
} else {
echo "Failed to move the file.";
}In this example, the example.txt file is moved to the example_directory directory.
Conclusion
The rename() function in PHP is a useful function for managing files and directories in your PHP scripts. We hope that this article has given you a better understanding of how rename() works and how it can be used in your own projects.
Practice
What does the rename() function do in PHP?