How can PHP be used to ensure that all file types are downloaded instead of opened in the browser when clicked?

When a user clicks on a file link, the browser may try to open the file instead of downloading it. To ensure that all file types are downloaded instead of opened in the browser, you can use PHP to set the appropriate headers in the response. By setting the "Content-Disposition" header to "attachment", you instruct the browser to download the file instead of displaying it.

<?php
$file = 'path/to/your/file.ext';

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));

readfile($file);
exit;
?>