What are the best practices for renaming files using PHP?

When renaming files using PHP, it is important to follow best practices to ensure that the operation is successful and secure. One common approach is to use the `rename()` function provided by PHP, which allows you to change the name of a file or directory. Additionally, it is recommended to check if the file exists before renaming it, handle any errors that may occur during the renaming process, and consider adding some form of validation to prevent malicious input.

$oldFileName = 'old_file.txt';
$newFileName = 'new_file.txt';

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