What potential issues can arise when using the PHP rename() function to move files between directories?

One potential issue that can arise when using the PHP rename() function to move files between directories is that the function may not work if the source and destination directories are on different filesystems. To solve this issue, you can use the copy() function to copy the file to the new directory and then unlink() to delete the original file.

// Check if the file exists
if (file_exists($source)) {
    // Copy the file to the new directory
    if (copy($source, $destination)) {
        // Delete the original file
        unlink($source);
        echo "File moved successfully.";
    } else {
        echo "Failed to move file.";
    }
} else {
    echo "File does not exist.";
}