Are there any common pitfalls to be aware of when renaming files using PHP?

One common pitfall when renaming files using PHP is not checking if the file already exists with the new name, which can result in overwriting existing files. To avoid this, you should first check if a file with the new name already exists before renaming the file.

$oldFileName = 'oldfile.txt';
$newFileName = 'newfile.txt';

if (!file_exists($newFileName)) {
    if (rename($oldFileName, $newFileName)) {
        echo 'File renamed successfully.';
    } else {
        echo 'Error renaming file.';
    }
} else {
    echo 'A file with the new name already exists.';
}