What are the potential pitfalls or best practices when using the copy() function in PHP?
When using the copy() function in PHP to duplicate files, it is important to handle errors that may occur, such as file permission issues or destination file already existing. It is also recommended to check if the copy operation was successful before proceeding with any further actions.
$sourceFile = 'file.txt';
$destinationFile = 'new_file.txt';
// Copy file with error handling
if (copy($sourceFile, $destinationFile)) {
echo "File copied successfully.";
} else {
echo "Error copying file.";
}