Temporary Internet Files\Content.IE5\ could not be found

Problem:

I created a restricted file download area for a client website, for their clients to download spreadsheets from their site. A problem arose where I got an error message (in Windows) when opening the file directly in Internet Explorer 6, and choosing ‘open’ instead of ‘save’ on the File Download popup. But when opening the same link in Firefox, it opens successfully! This is the error message one gets:

‘C:\Documents and Settings\\Local Settings\Temporary Internet Files\Content.IE5\’ could not be found

It also seems to be an issue in some other Microsoft software (such as Outlook) as well. The error was not given in Internet Explorer, but in whatever program is set to open the file, such as OpenOffice or Excel. The error message differs a little according to the program used, but the problem is that the temporary file cannot be found.

Solution:

After trying many things, I found that the problem I was experiencing has to do with cache headers. In PHP, one can stop the caching of content by serving headers similar to:

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

But when a site’s cache headers are set to not cache the content in that way, it seems that some Microsoft products don’t save the temporary file at all. The program then tries to open a file which just doesn’t exist!

To see it in action, try to open this file in Internet Explorer. When prompted to “open” or “save” the file, choose “open” — when a program like Excel or OpenOffice opens the document, an error message is received.

The solution is to instruct the user’s computer to cache the file for a short amount of time, so that the temporary file is created and the program has something to open. When one now tries to open the same file (in Internet Explorer), but with the cache headers in place, the file opens successfully.

To cache a file for 30 seconds, and overwrite any previous cache directives, send the following headers with PHP :

header('Content-Type: application/vnd.ms-excel');
header("Content-Length: " . filesize($file));
header('Content-Disposition: attachment; filename="'.$filename.'"');

/* cache */
header("Date: " . gmdate("D, j M Y H:i:s ", time()) . 'GMT');
header("Expires: " . gmdate("D, j M Y H:i:s", time() + 30) . " GMT");
header("Cache-Control: public"); // HTTP/1.1
header("Pragma: public");        // HTTP/1.0

readfile($file);

It would be interesting to know whether this is only a problem in Windows and Internet Explorer, or in other browsers or operating systems as well. Please leave a comment and let everyone know whether directly opening (instead of first saving and then opening locally) the problem file also gives you an error (or not), and which operating system and browser you used!