What are some best practices for handling file downloads in PHP, such as setting appropriate Content-Type headers?

When handling file downloads in PHP, it is important to set appropriate Content-Type headers to ensure that the browser interprets the file correctly. This can be done using the header() function in PHP to specify the content type of the file being downloaded. Additionally, it is good practice to set the Content-Disposition header to prompt the browser to download the file instead of displaying it in the browser window.

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