Are there any best practices for renaming and copying files using PHP scripts?

When renaming or copying files using PHP scripts, it is important to handle errors and ensure that the operations are performed securely. One best practice is to check if the file exists before renaming or copying it to avoid overwriting existing files. Additionally, using the PHP `rename()` function for renaming files and `copy()` function for copying files can help streamline the process.

// Rename a file
$oldFileName = 'example.txt';
$newFileName = 'new_example.txt';

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

// Copy a file
$sourceFile = 'example.txt';
$destinationFile = 'copy_example.txt';

if (file_exists($sourceFile)) {
    if (copy($sourceFile, $destinationFile)) {
        echo 'File copied successfully.';
    } else {
        echo 'Error copying file.';
    }
} else {
    echo 'Source file does not exist.';
}