Jump to content

Forced download not working in IE


ober

Recommended Posts

I am generating a snapshot of some data and giving the users the ability to download that snapshot.  I am trying to serve that file to the users and it works great in Firefox, but the IE users just get a blank file.  I'm using the following:

 

<?php
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Pragma: no-cache');
header('Content-Type: text/plain; charset=ISO-8859-1'); // plain text file
$filename = 'C:\WINDOWS\Temp\snapshot.txt';
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
header("Content-Length: ".filesize($filename));
readfile("$filename");
?>

 

I've changed multiple items several times and each works in FF, but none work in IE.  Is anyone aware of any IE specific headers that need to be sent?  It's just a plain text file, nothing fancy.

Link to comment
https://forums.phpfreaks.com/topic/211173-forced-download-not-working-in-ie/
Share on other sites

IE has a bunch of problems with headers. I have had a similar problem before, and my solution was to use a separate download link on the page they view the file results. Try this:

 

<a href="<?php echo $filename;?>">For IE users, click here to download the file</a>

 

 

I can't do that because the file isn't in the web server's path.  I'm actually grabbing it from outside of the public access.  So I have to be able to do this.  I guess my other option would be to copy it to another location after processing it, but it seems to be insane that this isn't possible.  It works great in FF...  :'(

Not this problem again.

 

I think the problem has to do with your web server outputting a content-type header for a .php file and/or IE automatically treating .php files the way it wants instead of the way you want.

 

Are you on an Apache web server so that you could use url rewriting to make it look like you are not using a .php file as the URL in the download link?

Nope, IIS.  And here's the weird thing.  When I browse on the server and go to the properties of the folder, the 'read only' checkbox is 'half-checked', if you know what I mean.  I uncheck it, hit ok, come back in and it's back to 'half-checked'... like I never touched it.

 

But the weird thing is, the temp folder where I was able to write to also has the same setting.

OK, it gets even more weird.  I can create an xlsx file in another directory and I can create a .dat file in this directory, but now the .dat file gets served as an empty file, even though I can see it and there is definitely data in it.

<?php
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Pragma: no-cache');
header('Cache-Control: private',false);
header('Content-Type: binary'); // plain text file
$filename = 'test.dat';
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
header("Content-Length: ".filesize($filename));
readfile("$filename");
?>

 

Well, I wrote the results to a .dat file and now it is serving it appropriately.  WEIRD.  Oh well, all is well that ends well.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.