Using PHP rename() to Rename Files and Directories
This article explains PHP's rename() function, detailing its syntax, parameters, return values, and provides example code for renaming both files and directories, along with important usage considerations, including error handling and permission requirements.
Introduction: In PHP, the rename() function is used to rename files or directories, providing a simple way to change their names by specifying the source and target.
Syntax: bool rename ( string $source , string $target )
Parameters: $source (required) – the name of the source file or directory; $target (required) – the desired new name.
Return value: Returns TRUE on success, FALSE on failure.
Examples:
Rename a file:
Rename a directory:
Note: The rename() function may be limited by filesystem permissions; ensure you have sufficient rights and that the source exists, otherwise the function returns FALSE .
Summary: The rename() function is a powerful PHP tool for renaming files and directories, allowing developers to change names by specifying source and target paths, as demonstrated in the example code.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.