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);

6 thoughts on “Display a download dialog for pdf in PHP

  1. 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. 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. 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. 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. 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. 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.

Comments are closed.

Twitter
LinkedIn
YouTube
Instagram