Take the 2-minute tour ×

so attempted this:

header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $fields;

tried also and many others.


what happens however is, the MySQL data simply echos out to the browser.

this is the HTTPheaderResponse i grabbed with firebug:

Date    Wed, 18 May 2011 14:15:18 GMT
X-Powered-By    PHP/5.1.6
Content-disposition attachment;filename=MyVerySpecial.csv
Connection  close
Content-Length  992
Server  Apache/2.2.3 (Red Hat)

EDIT: note it is a return from a JQuery .post() I am trying this, if that affects it.

so, does anyone know why this issue may be occuring? I am at a loss.


share|improve this question
do you have any output before headers? your code is okay and should work –  Nemoden May 19 '11 at 11:04
well, if look at my latest edit, I am attempting this as the return of a JQuery .post(), if that may be affecting it? –  buymypies May 19 '11 at 11:06
it should be just a link. Why do you need jquery.post? –  Nemoden May 19 '11 at 11:08
well its an extra feature off a page, and so I am querying the database to go into the file, without refreshing the page. –  buymypies May 19 '11 at 11:09
lol. Uh yeah, that would have been important information to have. : ) Your best bet is to use an actual form and do a traditional post... which can be done to an iFrame to maintain AJAX-like behavior. –  John Green May 19 '11 at 11:11

3 Answers 3

Proper MIME type is text/csv. Try that.

share|improve this answer

First of, be sure to set the second parameter of header() to true, it is to force setting the header:


Give that you know the size of the download, you should:

header('Content-Length: '.strlen($fields));

Finally, be sure that $fields is actually a string in CSV format.

You can't print an array directly and expect the browser to automagically convert it to CSV.

share|improve this answer
it is in csv format, constructed it previous. –  buymypies May 19 '11 at 11:07

Try marking the Content-type as either application/download or application/force-download. That should force the browser to open the save dialogue.

share|improve this answer
tried both. didnt change :( –  buymypies May 19 '11 at 11:02

Your Answer


