Web Development Blog

Twitter

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:

3 Responses to "Display a download dialog for pdf in PHP"

1 | Karen

July 30th, 2009 at 2:43 pm

Avatar

Hi,

I work for a web hosting site, which gives detailed information about different services and packages about hosting.

I came across your website : eisabainyo.net. And I find it very relevant to my client’s site.

I would like to have your co-operation, which I believe will help us to increase the productivity of both our sites.

If you are interested do get back to me. Awaiting for your positive reply.

Regards,
Karen

2 | hosting

August 31st, 2009 at 5:35 am

Avatar

I’m using this code to force browser’s download dialog.I have problem only with IE 6 because the downloaded file size is 0 bytes.

$fp=fopen(‘c:xampphtdocspesmeR.E.M.-Losing_My_Religon.mp3′,’r');
$name=basename(“c:xampphtdocspesmeR.E.M.-Losing_My_Religon.mp3″);
$size=filesize(‘c:xampphtdocspesmeR.E.M.-Losing_My_Religon.mp3′);
header(‘pragma: cache’);
header(‘pragma: public’);
header(‘Content-Type: audio/mpeg’);
header(‘Content-Length:$size’);
header(“Cache-Control: private”,false);
header(‘Cache-Control: max-age=0′);
header(“Content-Transfer-Encoding: binary”);
header(“Content-Disposition: attachment;filename= $name”);
fpassthru($fp);
fclose($fp);

3 | Pedro

October 5th, 2009 at 11:49 am

Avatar

i think i would need something similar to that for my website?

Can you give me a little hand?

I would like to let my visitors install the Greasemonkey scripts that i´ve hosted on my server. But instead of showing the ‘installation window’ shows the script code…..

Comment Form


Interests

Web development, Web design, Open source technologies, Portal development, APIs, Web services, Social media applications, Search engine optimisation, Mobile application development, iPhone Apps, Web 2.0, Web 3.0, Latest Internet technologies