What are the potential pitfalls of copying a file using PHP functions like copy() compared to fopen() and fwrite()?

When copying a file using PHP's copy() function, it reads the entire file into memory before writing it to the destination. This can be a problem when dealing with large files as it can consume a lot of memory. Using fopen() and fwrite() functions allows you to copy the file chunk by chunk, which is more memory-efficient.

$source = 'source.txt';
$destination = 'destination.txt';

$sourceFile = fopen($source, 'r');
$destinationFile = fopen($destination, 'w');

while (!feof($sourceFile)) {
    fwrite($destinationFile, fread($sourceFile, 8192));
}

fclose($sourceFile);
fclose($destinationFile);