note 99190 added to function.header
| From: | CodyG dot at osu1 dot php dot net | Date: | Mon, 02 Aug 2010 04:53:20 +0000 |
| Subject: | note 99190 added to function.header | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-171329@lists.php.net to get a copy of this message | ||
After lots of research and testing, I'd like to share my findings about my problems with
Internet Explorer and file downloads.
Take a look at this code, which replicates the normal download of a Javascript:
<?php
if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false) {
header("Content-type: text/javascript");
header("Content-Disposition: inline; filename=\"download.js\"");
header("Content-Length: ".filesize("my-file.js"));
} else {
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=\"download.js\"");
header("Content-Length: ".filesize("my-file.js"));
}
header("Expires: Fri, 01 Jan 2010 05:00:00 GMT");
if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false) {
header("Cache-Control: no-cache");
header("Pragma: no-cache");
}
include("my-file.js");
?>
Now let me explain:
I start out by checking for IE, then if not IE, I set Content-type (case-sensitive) to JS and set
Content-Disposition (every header is case-sensitive from now on) to inline, because most browsers
outside of IE like to display JS inline. (User may change settings). The Content-Length header is
required by some browsers to activate download box. Then, if it is IE, the
"application/force-download" Content-type is sometimes required to show the download box.
Use this if you don't want your PDF to display in the browser (in IE). I use it here to make
sure the box opens. Anyway, I set the Content-Disposition to attachment because I already know that
the box will appear. Then I have the Content-Length again.
Now, here's my big point. I have the Cache-Control and Pragma headers sent only if not IE.
THESE HEADERS WILL PREVENT DOWNLOAD ON IE!!! Only use the Expires header, after all, it will require
the file to be downloaded again the next time. This is not a bug! IE stores downloads in the
Temporary Internet Files folder until the download is complete. I know this because once I
downloaded a huge file to My Documents, but the Download Dialog box put it in the Temp folder and
moved it at the end. Just think about it. If IE requires the file to be downloaded to the Temp
folder, setting the Cache-Control and Pragma headers will cause an error!
I hope this saves someone some time!
~Cody G.
----
Server IP: 69.147.83.197
Probable Submitter: 97.73.64.156 (proxied: 97.73.79.41)
----
Manual Page -- http://www.php.net/manual/en/function.header.php
Edit -- https://master.php.net/note/edit/99190
Del: integrated -- https://master.php.net/note/delete/99190/integrated
Del: useless -- https://master.php.net/note/delete/99190/useless
Del: bad code -- https://master.php.net/note/delete/99190/bad+code
Del: spam -- https://master.php.net/note/delete/99190/spam
Del: non-english -- https://master.php.net/note/delete/99190/non-english
Del: in docs -- https://master.php.net/note/delete/99190/in+docs
Del: other reasons-- https://master.php.net/note/delete/99190
Reject -- https://master.php.net/note/reject/99190
Search -- https://master.php.net/manage/user-notes.php