Backend Development 3 min read

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.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP rename() to Rename Files and Directories

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.

BackendPHPfilesystemfile managementrename
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.