How can PHP be configured to download a specific file as an attachment instead of displaying it as PHP?

To configure PHP to download a specific file as an attachment instead of displaying it as PHP, you can use the header() function to set the appropriate content type and attachment header. This will force the browser to download the file instead of displaying it in the browser window.

<?php
$file = 'example.pdf';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file) . "\"");
readfile($file);
exit;
?>