Example 1 — Rename a File
Create a file called old.txt and rename it to new.txt.
<?php
$dir = sys_get_temp_dir();
file_put_contents($dir . '/old.txt', 'Hello');
if (rename($dir . '/old.txt', $dir . '/new.txt')) {
echo 'Renamed', PHP_EOL;
}
echo 'old.txt exists: ', file_exists($dir . '/old.txt') ? 'yes' : 'no', PHP_EOL;
echo 'new.txt exists: ', file_exists($dir . '/new.txt') ? 'yes' : 'no', PHP_EOL;
unlink($dir . '/new.txt'); How It Works
- file_put_contents() creates old. txt with the text Hello.
- rename() changes the name from old. txt to new.txt and returns true.
- file_exists() shows that the old name is gone and the new name is there.
- unlink() deletes the test file at the end.

