Home >Backend Development >PHP Tutorial >PHP function introduction—rename(): Rename a file or directory
PHP function introduction—rename(): Rename a file or directory
Introduction:
In PHP, the rename() function is used to rename a file or directory. It provides an easy way to change the name of a file or directory. Whether it is a single file or an entire directory, you can use this function to perform a rename operation. The renaming process can be easily accomplished by specifying the name of the source file or directory and the target name.
Syntax:
bool rename (string $source, string $target)
Parameters:
Return value:
Returns TRUE on success and FALSE on failure.
Examples:
The following are some examples of using the rename() function to demonstrate how to rename files and directories.
<?php $old_name = "old_file.txt"; $new_name = "new_file.txt"; if(rename($old_name, $new_name)) { echo "文件重命名成功!"; } else { echo "文件重命名失败!"; } ?>
In the above example, we renamed a file named "old_file.txt" to "new_file.txt" . If the rename is successful, "File rename successful!" will be output, otherwise "File rename failed!" will be output.
<?php $old_name = "old_directory"; $new_name = "new_directory"; if(rename($old_name, $new_name)) { echo "目录重命名成功!"; } else { echo "目录重命名失败!"; } ?>
In the above example, we renamed a directory named "old_directory" to "new_directory". If the rename is successful, "Directory rename successful!" will be output, otherwise "Directory rename failed!" will be output.
Note:
Summary:
The rename() function is a powerful function in PHP for renaming files or directories. It allows you to change the name of a file or directory by simply specifying the name of the source file or directory and the target name. Using the sample code, you can easily understand how to use the rename() function in your PHP project to implement file and directory renaming operations.
The above is the detailed content of PHP function introduction—rename(): Rename a file or directory. For more information, please follow other related articles on the PHP Chinese website!