Generating csv file doesn't work with right click save-as
I have a script that generates csv data that is sent to the user along with a bunch of headers that tell the browser. CSV file. Everything works fine when users (left) click on the script link, they are presented with a download dialog with a file name ending in .csv and it suggests using excel or calc to open it. However, when users right-click and select Save As, they are saved with the name PHP script.
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 when users left click to save the file as personal_information_date.csv; when they right click they are saved as download.php. I am using FF3. Oddly enough, IE7 doesn't have this problem.
Any ideas?
Use mod_rewrite to alias the file from file.csv to file.php, this is a browser issue, not PHP, because when saving the file, it doesn't run it until it is saved.
So, let's summarize:
- Link to
personal_information_date.csv
- Create a rule
mod_rewrite
that forwardspersonal_information_date.csv
todownload.php
(ex:)RewriteRule ^personal_information_date.csv$ download.php
.
source to share
An HTTP client can ignore more than one content type header, the other two will be ignored - which one? Depends on the browser implementation, hence the different behavior. The correct mime type is text / csv, not application / octet-stream! Content download header is correct for download.
source to share
- I find that setting three different mimetypes doesn't help.
- what is $ val? Is this well-known content or user provided - for example, could it contain nasty characters (like ")" or even strings, like introducing new HTTP header lines?
- Take a look at the HTTP headers that come to the client. Either the built-in Firefox info, or using LiveHttpHeaders (a plugin that can be found on the Mozilla site - logs all HTTP headers) - I'm sure there are other plugins available for FF.
Hope it helps.
source to share