What are best practices for handling file downloads in PHP to ensure successful and complete downloads?

When handling file downloads in PHP, it is important to set the appropriate headers to ensure successful and complete downloads. This includes setting the content type, content length, and specifying the filename. Additionally, it is recommended to use readfile() function to efficiently output the file contents to the browser.

<?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;
?>