rename
Renames or moves a file.
Syntax
rename(string $from, string $to): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $from | string | The current path. |
| $to | string | The new path. |
Return value
bool`true` if the operation succeeded.
Examples
Renaming a file:
<?php
$old = sys_get_temp_dir() . '/ref-old.txt';
$new = sys_get_temp_dir() . '/ref-new.txt';
file_put_contents($old, 'data');
rename($old, $new);
var_dump(file_exists($old));
var_dump(file_exists($new));Output:
bool(false)
bool(true)Notes
To move a file, give another folder in the new path. Files uploaded through a form are moved with move_uploaded_file(), not this function.
See also
Related recipes
In the tutorial
- Reading and writing files — Working with files in PHP — writing, reading, appending, reading line by line, checking existence and deleting.