Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filestreaming problem in IE

Status
Not open for further replies.

skiflyer

Programmer
Joined
Sep 24, 2002
Messages
2,213
Location
US
I have a script, it generates a file on the servers filesystem (unnecessary I know, but it's the way it is right now)

Then I run the following...
Code:
    if (is_file($filepath)) {
      $len=filesize($filepath);
      if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
	$filename = preg_replace('/\./', '%2e', $filename,
				 substr_count($filename, '.') - 1);
      }
      header("Pragma: ");
      header("Cache-Control: max-age=30, no-cache ");//  header("Cache-Control: ");
      header("Content-type: application/octet-stream");
      header("Content-Disposition: attachment; filename=\"".trim(htmlentities($filename))."\"");
      header("Content-length: $len");
      header("Connection: close");
      if ($fp=fopen($filepath, "rb")) {
	fpassthru($fp);
	die;
      }
      else {
	myHeader();
	echo 'ERROR: Could not open '.$filepath;
	die;
      }
    }

Where $filepath is the full path to the file and $filename is what it says. (For the record I've also tried Content-type=text/plain)

This all works perfectly in Firebird... but in IE, if I choose save, it works fine. If I choose open it asks me again.... so I choose open again... then, once in a rare while it works, but mostly it says the file cannot be found (and lists out a file in the temporary internet files folder).

Tinkering right now as I write this it seems whether or not it works depends on how long I wait before clicking the second open... if I click it right away, it's a failure, if I wait, it works... so my guess is it's actually downloading the file there.

 
I'm not sure what's going wrong. But I'm also seeing some things in your code that I do not understand.

Why are you replacing all but the last "." with "%2e" in filenames for IE?

Why are you in general sending htmlentity tags in the filename?

Why are you sending a blank "Pragma:" header?


What happens if you try this version of the script:

Code:
$fp = fopen($filepath, 'rb');

if ($fp !== FALSE)
{
	$len=filesize($filepath);

	header('Pragma: ');
	header('Cache-Control: max-age=30, no-cache ');
	header('Content-type: application/octet-stream');
	header('Content-Disposition: attachment; filename="' . $filename . '"');
	header('Content-length: ' . $len);
	header('Connection: close');

	fpassthru($fp);
}
else
{
	myHeader();
	echo 'ERROR: Could not open '.$filepath;
}





Want the best answers? Ask the best questions!

TANSTAAFL!!
 
No change in the different script, though I can't say I expected one as htmlentities wasn't changing anything in my test case.


As to the rest of the questions... I'm replacing all but the last . because of a bug in IE. I wish I remember the details of the bug, I put that bit of code in ages ago... I do believe it has something to do with the way IE appends #'s to filenames when a file of the same name already exists, that or the way it determines file types, or maybe it was both.

I'm sending htmlentities in general due to the way this filename is generated and the values it must assume. However, neither of those two clauses are being tripped in my test case as the filename is a nice simple one.

The blank Pragma is a leftover from my first attempt at keeping IE from caching the results using Pragma: no-cache (but our proxy defeated that)... I just never deleted it.

Here's the kicker, I use this same chunk of code in three places... one to download large executables, and twice to download CSV files for excel. Both the CSV pages exhibit this problem, the large binaries, download like a charm.
 
You may just be running into the fact that IE tends to pay more attention to filename extensions than to MIME-types. Are you naming the CSV files "something.csv"?

What happens if you name the file "something.foo"?



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Nothing good... I made it licenseStats.foo, and associated .foo with emacs.

After clicking open twice, emacs opens up a new file in my temporary internet files named licenseStats[1].foo, a new blank file I mean rather than the one with the correct information.

I'm noticing now that if I remove the Cache control header it still asks me if I want to open it twice, but it at least opens it after the second click.

I'll have to dig around and see if IE has any known issues with that header.

Thanks for the help sleipnir.
 
I'm finding it listed as a feature of IE!

Although it's only supposed to affect SSL connections, which mine are not.


You know, we have two internal webservers here... the group administering the other one refuses to support Mozilla to the point that I can't navigate his page... I think I'm just going to have to drop support for IE from mine. Anyway, looks like I have a useable work around even if it's ugly.
 
I'm finding it listed as a feature of IE!

Although it's only supposed to affect SSL connections, which mine are not.


You know, we have two internal webservers here... the group administering the other one refuses to support Mozilla to the point that I can't navigate his page... I think I'm just going to have to drop support for IE from mine. Anyway, looks like I have a useable work around even if it's ugly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top