What are common reasons for the error message "No such file or directory" when using the rename function in PHP?

The error message "No such file or directory" typically occurs when the file or directory specified in the rename function does not exist. To solve this issue, you should ensure that the source file or directory exists before attempting to rename it. You can use the file_exists function to check if the file or directory exists before calling the rename function.

$source = 'path/to/source/file.txt';
$destination = 'path/to/destination/file.txt';

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