Mike
Mike

Reputation:

Creating a csv-file, doesn't work with right-click save-as

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?

Upvotes: 0

Views: 712

Answers (3)

Arne Burmeister
Arne Burmeister

Reputation: 20604

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.

Upvotes: 1

Olaf Kock
Olaf Kock

Reputation: 48067

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

Upvotes: 0

Ross
Ross

Reputation: 47057

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).

Upvotes: 2

Related Questions