What are common pitfalls to avoid when implementing download scripts in PHP for large files?

Common pitfalls to avoid when implementing download scripts for large files in PHP include not setting appropriate headers, not handling file chunking for better performance, and not checking for file existence and permissions. To address these issues, ensure that headers are set correctly, implement file chunking to improve download speed, and validate file existence and permissions before allowing the download.

<?php

$file = 'path/to/large_file.zip';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . basename($file) . '"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));

    $handle = fopen($file, 'rb');
    while (!feof($handle)) {
        echo fread($handle, 4096);
    }
    fclose($handle);

    exit;
} else {
    echo 'File not found.';
}

?>