What are some best practices for handling file renaming in PHP to avoid errors like "No such file or directory"?

When renaming files in PHP, it is important to check if the file exists before attempting to rename it to avoid errors like "No such file or directory". This can be done using the `file_exists()` function to verify the existence of the file before renaming it.

$oldFileName = 'example.txt';
$newFileName = 'new_example.txt';

if (file_exists($oldFileName)) {
    if (rename($oldFileName, $newFileName)) {
        echo 'File renamed successfully.';
    } else {
        echo 'Error renaming file.';
    }
} else {
    echo 'File does not exist.';
}