Web Development Blog

by Ei Sabai Nyo

30 Jun, 2009

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);
Love what you've just read? Subscribe to our newsletter to receive tips, resources and special offers related to web development & design.
Your name:   Your email:  

6 Responses to “Display a download dialog for pdf in PHP”

  1. Karen says:

    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 says:

    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 says:

    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 ive hosted on my server. But instead of showing the ‘installation window’ shows the script code…..

  4. Shivangi says:

    Hello,

    I have one problem using the above snippet for pdf download is it does not support russian characterset i.e UTF-8. So, can anyone suggest me the solution for this.

  5. Nigel Nquande says:

    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.

  6. Nigel Nquande says:

    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.

    <?php
    $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); /* here’s your fix (var outside single quotes */
    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);
    ?>

    Ei, you need to fix your code : surround input from posts with htmlspecialchars(), because it ate the <?php … ?> code in my previous response.

Profile PicHello! Welcome to Web development blog! My name is Ei Sabai and on this blog, I write about web development, mobile app development, latest web technologies and the likes. Read more about me or have a look at some of the tips & resources I've written.
Subscribe to our newsletter to receive tips, resources and special offers related to web development & design.
We do NOT spam.
Your name:  
Your email:  

Tips & Resources

Tips & Resources
WordPress Web Hosting
Recommended web hosting providers for WordPress 3.0
iPhone Native App Development
Important steps into iPhone app development for beginners
iPhone Web App Development
Tips for iPhone web app development
Coupons for Web Developers
Get discounts on web hosting, domain names, templates, etc
10 Useful jQuery Snippets
Easy-to-use jQuery snippets for any website
HTML Email Newsletter
Step-by-step tutorial on how to code an HTML email newsletter
  • bluehost Hosting $6.95/month
  • Joomla Templates

Recommended Book

Categories