Backend Development 3 min read

PHP rename() Function: Syntax, Parameters, Return Values, Examples, and Usage Notes

This article explains PHP's rename() function, covering its syntax, parameters, return values, practical code examples for renaming files and directories, and important usage considerations such as permissions and error handling in PHP projects.

php中文网 Courses
php中文网 Courses
php中文网 Courses
PHP rename() Function: Syntax, Parameters, Return Values, Examples, and Usage Notes

In PHP, the rename() function is used to rename files or directories, providing a straightforward way to change their names 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.

Examples

Renaming a file:

This script attempts to rename old_file.txt to new_file.txt and outputs a success or failure message.

Renaming a directory:

The example shows how to rename a directory and report the result.

Notes

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

Summary

The rename() function is a powerful PHP tool for renaming files or directories by specifying source and target names, and the provided examples demonstrate its usage in typical PHP projects.

Backend DevelopmentPHPFilesystemfile handlingrename
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.