What are some best practices for handling file downloads and redirection in PHP scripts?

When handling file downloads and redirection in PHP scripts, it is important to set appropriate headers for file downloads and use proper redirection techniques to ensure a smooth user experience. To handle file downloads, you should set the content type header to the appropriate MIME type for the file being downloaded. For redirection, you can use the header() function to send a location header to redirect the user to a different page.

// Example for handling file download
$file = 'example.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
readfile($file);

// Example for redirection
header('Location: https://www.example.com');
exit;