Kishore Patra Kishore Patra - 1 year ago 89
PHP Question

What should be the Content-Type of a CSV file when downloading the MySQL data in a CSV file?

I am downloading some data from the database in the form of an Excel file (CSV format)..

I am using the header method to download the CSV file.

My code is as follows...

header("Content-Type: application/csv");
header("Content-Disposition: attachment; filename=export.csv");
header("Pragma: no-cache");
header("Expires: 0");

But after downloading the file and opening it, it shows " incorrect format" error. How do I download it in a proper format?

Answer Source

Since we don't know the tables nor the query acts as the source for the csv file and more importantly we don't know the data included in the CSV file, we can't give you accurate answer.

However keep in mind, that CSV is not a standardised format, therefore implementations may differ.

You can get the error message, because the software you are using has a different implementation (for example, it fails to parse multiline values, such as description texts).

Typical problems are:

  • text values are not qouted (and they can contain the separator character)
  • text values are qouted but not escaped correctly (some of them can contain the quotation character)
  • numeric values are exported in a localised format and the application reading the data is trying to parse them in another locale
  • some of the values are containing line endings and the software reading the data can not handle multi-line values

If you are assembling the CSV content by hand, be sure, that you are doing it in a way what can be handled by the target software.

Test your code (the CSV generation) using simple and short data to be sure, that it is working fine.

The content type of the CSV files is text/csv according to RFC4180.

PS: Marking your question as "unclear what you are asking", since the provided info is way not enough to solve and narrow down the problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download