Jump to content

HTTPS Download Tip for IE


obsidian

Recommended Posts

Well, I ran into an interesting situation in one of my apps the other day, and I figured I'd share it in case anyone else has had the same issue. I have a rather large application where many thousands of records are used to schedule some events. Recently, there has been a need to run some reports on the data and export CSV files for download. I develop in my testing environment and test in FF, IE and Opera, so I knew everything was running right. I had added the following headers to the function call to force download, and it was working perfectly:

<?php
header("Content-Type: application/txt");
header("Content-Disposition: attachment; filename=export.csv");
?>

 

Then, yesterday, I receive a call that one of the users was getting a strange error when they tried to download the report in IE. To make a long story short, it turns out that IE was choking only on production because of the protocol difference (HTTPS instead of HTTP like my testing environment). After much Googling and testing, I was finally able to come up with a very interesting header that seems to work just fine for almost any type of download both in HTTP and HTTPS:

<?php
header("Pragma: public");
header("Cache-Control: max-age=0");
header("Content-Type: application/octet-stream");
header("Content-Type: application/force-download", FALSE);
if (preg_match("/MSIE 5\.5/", $HTTP_USER_AGENT)) {
header("Content-Disposition: filename=export.csv");
} else {
header("Content-Disposition: attachment; filename=export.csv");
}
header("Content-Transfer-Encoding: binary");
?>

 

Apparently, IE picks up on the HTTPS protocol and needs to be told specifically that the file is OK.

 

I hope this is of some help to someone out there and I can save you a few minutes frustration that I went through.

 

Good luck!

Link to comment
Share on other sites

Thanks for the information. Perhaps you should move it to the FAQ/Code Snippet Repository, but then again, nobody seems to look there before posting.

Good call, I may do that.

 

Also, why do you send two Content-Types? The second one will override the first unless the $replace parameter (second parameter) is set to false (it's true by default).

Again, good catch. I intended to have them both run, so either the second needs the FALSE added, or else, I guess you could just remove the first one, since it appears to be working without it.

 

I went ahead and modified the initial post to reflect the FALSE parameter.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.