0

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

2
  • How about shortening that to just the relevant information for the question being asked? Commented Dec 21, 2012 at 20:19
  • 1
    FYI, your code is vulnerable to SQL injection. Commented Dec 21, 2012 at 20:22

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, Thanks a lot. I will ask my developer to review the security issues. I tried changing content type as text/csv, but when I try to export it just shows entire data in the web page itself.
That's actually normal. You will need to improve the script to do a forced download at that point. There are numerous ways to do that. You typically leave the option to download up to the user. It's just as easy to right-click on the link and select "save link as". Of course you could also just save the page (it will save as a csv).
2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.