Generate a CSV file using PHP

by on February 28, 2006

Q: How do you create a CSV file dynamically with PHP? I have tried many of the examples on the web but while the save/open prompt does show, it lets me know my file is unable to be opened. I need this to not be a file that is stored on my server but instead created at the time of demand.

A: The key to generating a CSV file using PHP that will be sent to your users web browser for them to open and do with as they want is to make sure you tell the browser that it is coming. Normally using PHP you just send the browser an html document and that is what the browser expects.

You need to tell the web browser to expect a file. In order to do this you have to use the header() functions built into PHP.

header(‘Expires: 0’);
header(‘Cache-control: private’);
header(‘Cache-Control: must-revalidate, post-check=0, pre-check=0’);
header(‘Content-Description: File Transfer’);
header(‘Content-Type: application/’);
header(‘Content-disposition: attachment; filename=”file_name.xls”‘);

Then you just echo your data. You have to make sure not to send any information before you call your header functions or else you will get errors on your page and the file will not be downloadable but will be displayed in the browser.

17 Responses to “Generate a CSV file using PHP”

    header(“Content-disposition: attachment; file_name.xls”);
    should be
    header(“Content-disposition: attachment; filename=file_name.xls”);
    otherwise your browser won’t use the file_name.xls given but instead the name of the page.


    This is not working with IE7. For some reason it is telling me that there is an error when trying to download the php file….works great with firefox though


    It is unfortunate that you did not use a real e-mail address Thad because I would have liked to have look into the issue related to IE7 with you but I guess that wont happen now.

    So for anyone else actually using IE7 that wants to help try to resolve the issue please use a real e-mail address so I can contact you.


    What is the IE fix? Is is possible to post the code?


    I don’t know the actual fix because I am not sure what the actual problem is. I was under the impression that it worked fine in IE7 as I use it on a few sites and have never had an issue.

    If you are using IE7 can you tell me what the issue is that you are having so I can look into it?


    Hi, I am having this error:

    Parse error: syntax error, unexpected ‘:’ in C:\Program Files\xampp\htdocs\TestSite\index2.php on line 43

    line 43: header(“Expires: 0”);

    any ideas?




    You might want to replace all the quotes because it is possible that they are microsoft type quotes that are getting copied over instead of ANSI quotes.

    If that does not fix the problem please let me know.


    It works now. I didn’t change anything. The answer was to open it from a link in stead of directly in the browser. So when I posted the above message i checked the link in it, it worked!!!
    I want to thank Matt anyway!!


    You are welcome and yes you are 100% correct when you say the key is to visit the page and download the document and save it on your computer. From there you can then open it in excel.

    Glad we could be of help!


    I am having IE issues. I am getting the error message ‘Internet Explorer cannot download from .
    Internet Explorer was not able to open this internet site. The requested site is either unavailable or can not be found, please try again later.

    It works fine in Firefox.


      Yeah IE is really a pain. It is even to the point where Google is now releasing code specifically to fix issues with IE so it will act like the standards say it should act. With that said, the code above should work fine on IE, at least up to version 7. I have not tried it with IE 8. What version of IE are you using?


    We test our code on all versions, and it does not work on any.


      I would have to see your code then because I use the code posted above daily with IE7 and have no issues at all.


        Should I email it to you? or how would you like to do it?


    By all versions, I mean 6+


    I have modified the code slightly to use single quotes instead of double quotes and to put double quotes around the file name. I have tested the new modified version and I have had no problems with it in OS X using FireFox or Safari or on Windows XP using FireFox or IE7.

    If anyone runs into issues I would suggest you test it with a small set of data, maybe one row to start, and see what happens.


    Phegan figured out the problem is with session_start() getting called before the header() functions with IE. He also figured out that the solution is to call session_cache_limiter(‘public’); before session_start(). I have not tested it but he has confirmed that this fixed his problem so it might help other people too.