30 Jun, 2009
Display a download dialog for pdf in PHP
Web Development » PHP, Snippets » Display a download dialog for pdf in PHP
In order to display a download dialog for pdf file rather than opening it in the browser, we can put the following snippet of code in a php file and name the file download.php.
The path to the pdf file is specified in $filename variable. You can also pass filename as a parameter in the URL but you will need to check for Cross Site Scripting (XSS) and various script injection attempts if you decide to get the filename from the URL paramater.
$filename = '/path/to/your/file/download.pdf';
header("Pragma: public");
header("Expires: 0");
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-disposition: attachment; filename=' . basename($filename));
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($filename));
@readfile($filename);
exit(0);
Other similiar posts that you might be interested in:
- Export into excel with formula in PHP
- Dreamweaver crashes during site cache build
- 25 Most Dangerous Programming Errors
- 10 Website Optimisation Tips for Web 2.0 Websites
- Firefox browser cache setting
- 5 must have plugins when using WordPress as a CMS
- Display a flash/image/text file randomly using javascript






