What are best practices for adjusting HTTP headers in PHP to ensure proper file downloading functionality?

When downloading files in PHP, it is important to set the appropriate HTTP headers to ensure proper functionality. This includes setting the Content-Type header to specify the type of file being downloaded, 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. Failure to set these headers correctly can result in unexpected behavior or errors when downloading files.

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