How can developers ensure cross-browser compatibility when handling file downloads in PHP?

Developers can ensure cross-browser compatibility when handling file downloads in PHP by setting appropriate headers in the response to indicate the file type and force the browser to download the file instead of displaying it. This can be achieved by using the header() function in PHP to set the Content-Type and Content-Disposition headers.

<?php
$file = 'example.pdf';

header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));

readfile($file);
exit;
?>