How does the PHP function copy() work and what considerations should be taken into account when using it for file operations?

The PHP function copy() is used to copy a file from one location to another. When using copy() for file operations, it is important to ensure that the source file exists and that the destination directory has the necessary permissions to write the copied file.

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

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