Reputation: 5042
I wrote PHP code to export data to .csv
file. The exported .csv
file was downloadable on my own PC. But in client's server, the .csv
file was not downloadable, it was in readable format (the browser can read and print the file contents like a .html
file).
I used header()
before accessing the .csv
file.
Is it a server side problem or do I need to change my code?
Upvotes: 2
Views: 8671
Reputation: 1828
I very much liked this solution. In case the URL isn't reachable (any more):
Put a .htaccess
file into the same folder and add this line to it
AddType application/octet-stream .csv
that's all. (The original Link has some more file extensions, but this is the one for the OP).
Upvotes: 1
Reputation: 31730
You need to send headers to tell the client what kind of file to expect and what to do with it.
header ('Content-Type: text/csv');
header ('Content-Disposition: attachment; filename="put_your_preferred_file_name_here.csv"');
Upvotes: 9
Reputation: 854
You need to change the header sent by the server to let know to the browser that it need to download it instead of show it.
Add this to make the file being open by excel
header("Content-type: application/vnd.ms-excel")
or this for a more standard type
header("Content-type: text/csv")
Upvotes: 4