CSV file - the browser opens it instead of downloading

I have written PHP code to export data to a file .csv. The exported file .csvcould be downloaded to my PC. But on the client server the file was .csvnot downloadable, it was in a readable format (the browser can read and print the contents of the file as a file .html).

I used header()before accessing the file .csv.

Is this a server side problem or do I need to change the code?

+1
source share
3 answers

You need to send headers to tell the client which 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"');
+9
source

, , , , .

, excel

header("Content-type: application/vnd.ms-excel") 

header("Content-type: text/csv")
+4

I really liked this solution . If the URL is unavailable (no more):

Put the file .htaccessin the same folder and add this line to it

AddType application/octet-stream .csv

what all. (There are several more file extensions in the source link, but this is for the OP).

+1
source

Source: https://habr.com/ru/post/1655919/


All Articles