How can PHP be used to control whether a file is displayed or offered for download?
To control whether a file is displayed or offered for download using PHP, you can set the appropriate HTTP headers before outputting the file contents. To offer a file for download, you need to set the Content-Disposition header to "attachment". To display a file in the browser, you can set the Content-Type header to the appropriate MIME type.
<?php
$file = 'example.pdf';
// Offer file for download
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
readfile($file);
// Display file in the browser
// header('Content-Type: application/pdf');
// readfile($file);
?>