CSV file - browser opens it instead of downloading

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"');

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

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

标签