tags:

views:

418

answers:

3

I have a script that generates data in csv format which is sent to the user along with a set of headers that tell the browser it is a .csv file. Everything works great when users (left)click on the link to the script, they are presented with a download dialog with the filename ending in .csv and it suggests using excel, or calc, to open it. However, when users right-click and choose Save As it is being saved with the php script name.

Here is the header code:

header("Pragma: public");
header("Expires: 0"); // set expiration time

header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");

$val = date("m_d_Y_g_i");
Header('Content-Disposition: attachment; filename="personal_information_'.$val.'.csv"');

So again, when users left-click it saves the file as personal_information_date.csv; when they right click it saves as download.php. I'm using FF3. Oddly enough, IE7 does not have this problem.

Any ideas?

+2  A: 

Use mod_rewrite to alias the file from file.csv to file.php, this is a browser issue rather than PHP because by saving the file it isn't running it before it is saving it.

So to summarise:

  1. Link to personal_information_date.csv
  2. Create a mod_rewrite rule that forwards personal_information_date.csv to download.php (e.g.: RewriteRule ^personal_information_date.csv$ download.php).
Ross
Let us know if this works for you, as it assumes you are using Apache and can turn on mod_rewrite
Liam
A: 
  • I believe that setting three different mimetypes doesn't help
  • what's $val ? Is this known content or user provided - e.g. could it contain nasty characters (like ") or even linebreaks, e.g. introduce new HTTP header lines?
  • have a look at the HTTP-Headers that arrive at the client. Either the Firefox built-in information or use LiveHttpHeaders (plugin to be found at the Mozilla site - logs all HTTP-Headers) - I'm sure there are more/other plugins for FF available.

Hope this helps.

Olaf
I think the Content-Type headers are there to deal with different browsers but I can't be sure.Val is set just before the last line:$val = date("m_d_Y_g_i");I've found this helps with caching problems because the filename is never the same.I'll take a look at the headers, thx for the tip.
oops - that $val assignment has been far too obvious for me to see :)Please let us know your results.
Olaf
+1  A: 

The HTTP client may ignore more than one content type header, the two other will be ignored - which of them? Depends on the browser implementation, therefor the different behaviour. The correct mime type is text/csv, not application/octet-stream! The content-disposition header is correct for the download.

Arne Burmeister
Thanks for the clarification about the multiple mime-types.. I knew I had them for a reason. I assume you meant text/csv -- not test/csv, right?.