What are some best practices for handling file downloads in PHP to prevent errors?

When handling file downloads in PHP, it's important to set the appropriate headers to prevent errors such as corrupted files or unexpected behavior. One common best practice is to use the `header()` function to specify the content type and disposition of the file being downloaded.

<?php
$file_path = '/path/to/file.pdf';

if (file_exists($file_path)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename=' . basename($file_path));
    header('Content-Length: ' . filesize($file_path));
    
    readfile($file_path);
    exit;
} else {
    echo 'File not found.';
}
?>