Internet Explorer -- Content-type and Content-Disposition?



  • Hello all... I've got a bit of a problem with IE6/7 handling "Content-type" and "Content-disposition" headers being passed from a PHP application which I wrote. When you click on the icon to download on the page, it calls another page (getpdf.php), which sorts out the location of the PDF file on the drive and calls the following three lines to actually deliver the PDF file:

                 //$sourcefile is the absolute path to the file on the hard drive.

                header('Content-type: application/pdf');
                header('Content-Disposition: attachment; filename="' . $cert. '.pdf"');
                readfile($sourcefile); 

    In Firefox, the download window comes up fine, asking what I want to do with the PDF (desired behavior). Under IE6/7, I instead get the download window in the background trying to download the getpdf.php file, with an error message "IE cannot download getpdf.php from www.quantumcalibration.com   IE was not able to open this Internet site."

    The same thing happens with another similar page (getcal.php), which sends out a .CAL file (data file used by the applications we develop in-house), but it uses "Content-type: application/octet-stream" instead... this gives me an Open/Save/Cancel dialog, but trying to open or save gives me the same "cannot download" error.

    Links to the error messages:     getpdf      getpdf

    Does IE not handle the Content-type or Content-disposition headers properly, or am I overlooking something? 



  • Well, on one of my sites I set the content type to have a php page deliver an excel spreadsheet.  I just tried it and it works fine on IE7.  If you want I get get the source for you in a little bit.



  • That would be great... I very well may be missing something.



  • I've once had a similar issue and using something like

     getpdf.php?x=.pdf
    solved it



  • The .PDF or .CAL extension is automatically added to the end of the
    $sourcefile path... the URL param is used to find the path to the file
    in a database and the path including the extension is dropped into the
    readfile().



  • Well, I looked at my code the first two lines are exactly the same as yours.  Guess that's not much help is it.  😕



  • What does $cert represent, and what is its typical value? Are you giving IE difficult characters for the suggested filename? Question marks or other forbidden punctuation, perhaps?

    Why are you using a variable for the suggested filename, anyway?

    Also, I assume you've sniffed the packets and checked the headers being received by IE6/7. Care to post an example?

    Finally, I believe it is usually considered best-practice to use application/octet-stream rather than the actual mime-type (application/pdf) for a forced file download.



  • How are you sending the data to the user? Are you using the readfile() function like I am?



  • I vaguely remember having a similar issue the last time I used PHP.  I dug up some of my code and here is what I used -- good thing I just happened to throw that CD in my bag this morning.  I don't remember whether this prompts for a download or actually displays the PDF inline - I haven't done PHP/Web stuff routinely in years.  If I remember correctly it was the "application-download" Content-type that did the trick.

        if (strstr($HTTP_USER_AGENT,"MSIE"))
        {
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Content-type: application-download");
            header("Content-Length: $size");
            header("Content-Disposition: attachment; filename=MyPDF.pdf");
            header("Content-Transfer-Encoding: binary");
        }
        else
        {
            //header("Pragma: public");
            //header("Expires: 0");
            //header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Content-type: application-download");
            header("Content-Length: $size");
            header("Content-Disposition: attachment; filename=MyPDF.pdf");
            //header("Content-Transfer-Encoding: binary");
        }

        readfile($file);
     



  • @Whiskey Tango Foxtrot? Over. said:

    What does $cert represent, and what is its typical value? Are you giving IE difficult characters for the suggested filename? Question marks or other forbidden punctuation, perhaps?

    Why are you using a variable for the suggested filename, anyway?

    $cert is the unique number assigned to each individual certificate (one .CAL, one .PDF for each number).  


    Also, I assume you've sniffed the packets and checked the headers being received by IE6/7. Care to post an example?

    Negative.. don't have a packet sniffer installed on my workstation right now.

     

    Finally, I believe it is usually considered best-practice to use application/octet-stream rather than the actual mime-type (application/pdf) for a forced file download.

    application/octet-stream doesn't work either... see my problem with my getcal.php file as well, which uses octet-stream. 




  • Could this have anything to do with a permissions issue or something? I also have a script which copies a series of files (array of filenames) from a bunch of folders into one folder, then zips the combination folder, and that isn't working either.

    All of these scripts used to work, but now they're all misbehaving after the head of IT decided to get rid of the domain that the server was on and make that network a workgroup (don't ask the logic... ), so I suspect that might have something to do with it. And ideas?



  • @lpope187 said:

        if (strstr($HTTP_USER_AGENT,"MSIE"))
        {
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Content-type: application-download");
            header("Content-Length: $size");
            header("Content-Disposition: attachment; filename=MyPDF.pdf");
            header("Content-Transfer-Encoding: binary");
        }
        else
        {
            header("Content-type: application-download");
            header("Content-Length: $size");
            header("Content-Disposition: attachment; filename=MyPDF.pdf");
        }

        readfile($file);
     

    Bingo. I haven't a clue which part actually did it (just changing it to application-download in my code didn't do it), but it does indeed work.


    Thank you, sir.
     



  • I had the same problem, and this fixed it, too, thanks a lot!

     

    Anyone have ideas on why Internet Explorer behaves this way?



  • @mentalbastille said:

    Anyone have ideas on why Internet Explorer behaves this way?
    Because you resurrect 18-month-old threads?



  • Hey, the thread is already resurrected, so this is OK, right?

    @mentalbastille said:

    Anyone have ideas on why Internet Explorer behaves this way?

    IE examines a resource's extension (as well as contents) to help itself determine how to handle it. AFAIK, this is done to "help" users who can't configure their webservers properly, but unfortunately leads to haemorrhoids like this issue. I've been bitten several times by IE interpreting plain text files as something wicked (and refusing to open them at all) because of their extension.

    [url=http://blogs.msdn.com/ie/archive/2005/02/01/364581.aspx]This[/url] is how it was done, and [url=http://blogs.msdn.com/ie/archive/2008/07/02/ie8-security-part-v-comprehensive-protection.aspx]here[/url] are some changes introduced in IE 8. There are also some interesting registry settings documented [url=http://technet.microsoft.com/en-us/library/cc787872.aspx]here[/url], albeit I wasn't able to turn MIME sniffing off with them.



  •  Cool, thanks for the info!

     

    Sorry for the thread necro, blame google, it got me here 😕



  • @mentalbastille said:

    Sorry for the thread necro, blame google, it got me here 😕

    So I guess "Do no evil" is out now.. 



  •  so i have a php file that creates a string and returns it as a .csv file with this code:

    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=$file");
    file_put_contents("php://output", $str);

    on the client side, i call it with:

    window.open(download);

    where download is the url + get data that needs to be sent.  in firefox, everything works fine.  in ie, even that fix above doesn't seem to make any difference.  i understand that ie is doing some sniffing, but i don't know what i can do to make it recognize the .csv file.



  • @ruperik said:

    on the client side, i call it with:

    window.open(download);


    try with:


    location.href = download;
    


  •  that doesn't work



  • @ruperik said:


    header("Content-Disposition attachment; filename=$file");
    If that’s a direct copypaste from your application, your Content-Disposition header is invalid. You missed a colon.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.