Page 1 of 1

Streaming and IE

Posted: Tue Sep 05, 2006 7:06 am
by HardlyNoticable
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi There,

I'm building a web based upload/download app for my work in PHP.  We have a lot of clients that we transfer files back and forth with and have some very specific processes that we wish to employ.

For downloading, I am streaming the file to the client's web browser (this way we can control access and do application level logging).  However I am having a problem getting it to work properly in IE.

In IE, if the user saves the file, everything works fine.  But if the user opens the file directly, the browser downloads the file and then doesn't open it.  If the file is a PDF, the IE downloads the file, and acrobat reader starts, and then says it can't find the file.  I suspect the file is downloading under one name (probably the correct one) and then the browser or reader is trying to open it under another name (probably the name of the php page that's streaming the file).  

Has anyone here ever encountered this problem?  I suspect it may have something to do with the header that I'm sending to the client's browser.  I should mention that Firefox and Safari are not having any issues (only IE) and that I have tried this on multiple machines.

The following is the code I'm using for the actual download:

Code: Select all

...
else // Usual download
{
   header("HTTP/1.1 200 OK");
   header("Content-Length: $filesize");
   header("Content-Type: application/force-download");
   header("Content-Disposition: attachment; filename=$root_filename");
   header("Content-Transfer-Encoding: binary");

   if(file_exists($filepath) && $fh = fopen($filepath, "rb")){
       while($buf = fread($fh, $bufsize))
           print $buf;
       fclose($fh);
	   set_activityrecord(0, "File downloaded - no error.");
   }
   else
   {
       header("HTTP/1.1 404 Not Found");
	   set_activityrecord(3, "File not found on download.");
   }
}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Sep 05, 2006 7:20 am
by feyd
It's possible IE doesn't like/understand your content-type.

Posted: Thu Sep 07, 2006 1:02 pm
by HardlyNoticable
I tried using the following header and ran another test (while downloading a PDF):

Code: Select all

header("HTTP/1.1 200 OK");
   header("Content-Length: $filesize");
   header("Content-type: application/pdf");
   header("Content-Disposition: attachment; filename=$root_filename");
   header("Content-Transfer-Encoding: binary");
And I got the same behaviour with IE when I tried to open the file (not save) with IE (From acrobat reader):
There was an error opening this document. This file cannot be found.
Again, I am sure that it IS actually downloading the file (it just can't seem to find it after it's downloaded) AND it does work when I save the file and THEN open it.

Does anyone else have any suggestions? I'd include the entire page, but I think the code segment from my first message has everything relative to the situation. If however you would like to see it all, just let me know.

Solution

Posted: Thu Sep 07, 2006 1:38 pm
by HardlyNoticable
I found a solution! (yay!)

I added these lines to my header (probably only one of the three fixed the problem; but I'm a lazy programmer and "if it ain't broke, don't fix it" is my moto):

Code: Select all

header("Pragma: public");
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("Cache-Control: private",false);
Now it works in IE, Firefox (PC and Mac) and Safari (PC and Mac).

Posted: Thu Sep 07, 2006 2:09 pm
by feyd
Safari's on PC?

oops

Posted: Thu Sep 07, 2006 9:20 pm
by HardlyNoticable
oops, I got a little over enthusiastic. Safari on the Mac (only).