Home > Backend Development > PHP Tutorial > PHP function introduction—rename(): Rename a file or directory

PHP function introduction—rename(): Rename a file or directory

WBOY
Release: 2023-07-25 12:12:01
Original
2750 people have browsed it

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:

  • $source: required, indicating the source file or directory name.
  • $target: required, indicating the name of the target file or directory.

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.

  1. Rename a file:
<?php
$old_name = "old_file.txt";
$new_name = "new_file.txt";

if(rename($old_name, $new_name)) {
    echo "文件重命名成功!";
} else {
    echo "文件重命名失败!";
}
?>
Copy after login

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.

  1. Rename a directory:
<?php
$old_name = "old_directory";
$new_name = "new_directory";

if(rename($old_name, $new_name)) {
    echo "目录重命名成功!";
} else {
    echo "目录重命名失败!";
}
?>
Copy after login

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:

  • The rename() function may be restricted by file system permissions when executed. Please make sure you have sufficient permissions to change the name of the file or directory.
  • If the source file or directory does not exist, the rename() function will return FALSE, indicating that the rename failed.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template