What are the best practices for using the PHP rename() function when moving files within a web application?

When using the PHP rename() function to move files within a web application, it is important to check if the file exists before attempting to move it. Additionally, make sure to handle any errors that may occur during the file moving process. It is also a good practice to use absolute file paths to ensure the correct file is being moved.

$oldFilePath = '/path/to/old/file.txt';
$newFilePath = '/path/to/new/file.txt';

if (file_exists($oldFilePath)) {
    if (rename($oldFilePath, $newFilePath)) {
        echo 'File moved successfully.';
    } else {
        echo 'Error moving file.';
    }
} else {
    echo 'File does not exist.';
}