Skip to content
PHP Logo

How can PHP be used to prompt users to save a file instead of opening it in the browser window?

When a file is accessed through a web browser, the browser may try to display the file directly instead of prompting the user to save it. To force the browser to prompt the user to save the file instead of opening it, you can use PHP headers to set the content type and disposition.

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