How can the header function in PHP be used to address problems with file downloads in different browsers?

The header function in PHP can be used to address problems with file downloads in different browsers by setting the appropriate content type and content disposition headers. This ensures that the browser knows how to handle the file being downloaded, such as prompting the user to save the file or opening it in a specific application.

<?php
$file = 'example.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
readfile($file);
?>