What are the common pitfalls to avoid when working with file downloads in PHP?

One common pitfall when working with file downloads in PHP is not setting the correct headers for the file type and attachment disposition. To avoid this issue, make sure to set the "Content-Type" header to the appropriate MIME type of the file being downloaded and set the "Content-Disposition" header to "attachment" to prompt the browser to download the file instead of displaying it.

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