rename()

rename() — Renames a file or directory.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

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.

Signature

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

Parameters

ParameterTypeWhat it takes
$fromstringThe old name.
$tostringThe new name. Note: On Windows, if to already exists, it must be writable. Otherwise rename() fails and issues E_WARNING.
$context?resourceA context stream resource.

Return value

Returns true on success or false on failure.

Example

A runnable snippet:

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

See also

copy(), unlink(), move_uploaded_file()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page rename() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.