Forcing PDF files to download

Recently, I was dealing with a rather dumb issue between various browsers and how they display/download PDF files. What was dumb was that one browser would display an error when the file originates from a CDN, while when locally available on the same server, no issues. Let’s not forget, visa versa for the other browser.

First thing I managed to do was reach out to the browser and CDN in question on twitter, hoping this was a known issue. With my luck, no go. So, let’s just force a file download and avoid the headache all in all. We’ll call this getfile.php.

You first need to create a page that will be referenced, which will initiate the file download. In this example, we pass in the name of the file without the extension.

    header("Content-Type: application/octet-stream");

    $file = "".$_GET["file"].".pdf";
    $name = $_GET["file"].".pdf";
    header("Content-Disposition: attachment; filename=".urlencode($name));
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Description: File Transfer");            
    header("Content-Length: ".filesize($file));


    $fp = fopen($file, "r");
    while (!feof($fp)) {
        echo fread($fp, 65536);


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>