Dev Guy
Dev Guy

Reputation: 520

PHP CSV Export Issue

I am trying to export MySQL data to CSV file. It works fine on all browsers, but having issue only on MAC system. The file gets downloaded as filename.csv.xls which should be filename.csv I have tried all possible solutions but nothing seems to work. am I missing something here? Below is the source code.

<?php
header("Content-type: application/vnd.ms-excel"); 
header("Content-Disposition:csv" . date("Y-m-d")); 
header( "Content-disposition: filename="file.csv");
print $csv_output;
exit;
?>

Thanks

Upvotes: 0

Views: 272

Answers (2)

Mark Baker
Mark Baker

Reputation: 212412

Perhaps if you used the correct content type in the headers

text/csv

rather than the Excel .xls content type header

application/vnd.ms-excel

Upvotes: 2

Jeremy
Jeremy

Reputation: 1972

It may be your content-type header. Try this instead:

header("Content-type: text/csv");

A security note: I also noticed you are open to SQL injection. You should always pass your variables through mysql_real_escape_string especially since you are not even sanitizing the data. You are also using mysql instead of the improved mysqli.

Upvotes: 2

Related Questions