What are some best practices for handling file downloads in PHP to prevent corrupted files?

When handling file downloads in PHP, it is important to set the appropriate headers to ensure the file is downloaded correctly and prevent corruption. This includes setting the content type, content length, and content disposition headers. Additionally, using functions like `readfile()` to read and output the file can help prevent corruption during the download process.

$file = 'path_to_your_file.pdf';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . basename($file) . '"');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
} else {
    echo 'File not found';
}