Backend Development 3 min read

Using PHP rename() Function to Rename Files and Directories

This article explains PHP's rename() function, detailing its syntax, parameters, return values, and usage examples for renaming both files and directories, while also highlighting important considerations such as permission constraints and error handling.

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

In PHP, the rename() function is used to rename a file or directory, providing a straightforward method to change its name by specifying the source and target names.

Syntax:

bool rename ( string $source , string $target )

Parameters:

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

$target : required, the name of the destination file or directory.

Return value:

Returns TRUE on success and FALSE on failure.

Example – renaming a file:

Example – renaming a directory:

Note: The rename() function may be restricted by filesystem permissions; ensure you have sufficient rights. If the source file or directory does not exist, the function returns FALSE .

Summary: The rename() function is a powerful PHP tool for renaming files and directories, allowing developers to change names simply by providing source and target paths.

BackendPHPfile handlingdirectoryrename
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.