What are some best practices for handling file downloads in PHP to ensure compatibility with different browsers?

When handling file downloads in PHP, it's important to set the appropriate 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. Additionally, using ob_clean() and flush() functions can help prevent any output buffering issues that may interfere with the file download.

<?php
// Set headers
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"example.txt\"");
header("Content-Length: " . filesize("example.txt"));

// Clear output buffer
ob_clean();
flush();

// Output the file
readfile("example.txt");