What are best practices for setting up HTTP headers when sending binary files for download in PHP, especially considering compatibility with different browsers?

When sending binary files for download in PHP, it is important to set the appropriate HTTP headers to ensure compatibility with different browsers. This includes setting the Content-Type header to specify the file type, Content-Disposition header to prompt the browser to download the file instead of displaying it, and Content-Length header to indicate the size of the file being sent.

$file_path = 'path/to/your/file.pdf';

// Set the appropriate headers
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($file_path) . '"');
header('Content-Length: ' . filesize($file_path));

// Read the file and output it to the browser
readfile($file_path);