php8.5
Home/ Manual/ filesystem / functions/ rename

rename

PHP function Edit on GitHub ✎

(PHP 4, PHP 5, PHP 7, PHP 8)

Renames a file or directory

Description

rename(string $from, string $to, resource|null $context = null): bool

Attempts to rename from to to, moving it between directories if necessary. If renaming a file and to exists, it will be overwritten. If renaming a directory and to exists, this function will emit a warning.

Parameters

from

The old name.

Note

The wrapper used in from must match the wrapper used in to.

to

The new name.

Note

On Windows, if to already exists, it must be writable. Otherwise rename() fails and issues E_WARNING.

context

Context-support

Return Values

Success

Examples

Example with rename()

php
<?php
rename("/tmp/tmp_file.txt", "/home/user/login/docs/my_file.txt");
?>

See Also

Source: reference/filesystem/functions/rename.xml · from the official PHP manual (php/doc-en)