CSV Download File Always Empty

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ouray897
Forum Newbie
Posts: 3
Joined: Wed Mar 07, 2012 11:49 pm

CSV Download File Always Empty

Post by ouray897 »

I have created a routine that exports report data to a csv file and the end result is an empty CSV file upon download. I can run this code in the Zend and LAMPP locally and it works great. The CSV file is pipe-delimited but I don't think that is the problem. I starting to think that it has something to do with a setting on my server, Host Gator, which is using PHP 5.2.17. Does anyone know of any options in the PHP configuration that might prevent a download from working using the header() functions? Here is a shortened version of my code with all ob*, header*, and file operation statements. As I said before, this works fine locally on my laptop in either the Zend or XAMP/LAMPP server.

Code: Select all

        error_reporting(0);
        ob_start();

        $csvfilename = 'nielsen-'.$country.'-'.Date('Y-m-d-His', time()).'.csv';

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private",false);
        header("Content-Type: text/csv");
        header("Content-Disposition: attachment; filename=$csvfilename");
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: ".filesize($csvfilename));

        $outstream = fopen('php://output', 'w');
        $header = "HEADER RECORD";
        fwrite($outstream, $header);
        ...
        //code shortened here - not actual while loop statement
        while (loop) {
              fputcsv($outstream, $record, '|');
        }
   
        ob_end_flush();
        fclose($outstream);

User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: CSV Download File Always Empty

Post by tr0gd0rr »

It looks like you are outputting a Content-Length of 0. If you want to output the correct content length, you would need to use ob_start(), ob_get_clean(), strlen then add Content-Length header then echo.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: CSV Download File Always Empty

Post by requinix »

tr0gd0rr wrote:It looks like you are outputting a Content-Length of 0. If you want to output the correct content length, you would need to use ob_start(), ob_get_clean(), strlen then add Content-Length header then echo.
...unless the size of the $csvfilename file isn't zero, in which case the Content-Length will be whatever the length of the file is. :?
If you're outputting a file, Content-Length with filesize() and readfile() are all that you "need".
ouray897
Forum Newbie
Posts: 3
Joined: Wed Mar 07, 2012 11:49 pm

Re: CSV Download File Always Empty

Post by ouray897 »

I have tried everything that both of you have said. I even tried using echo and print instead of fwrite and fputcsv, to no avail. At the point the download occurs, it should have at least the header record in it, which is not based on anything pulled from the database. Even if I just echo a simple string with text/plain, I get an empty file.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: CSV Download File Always Empty

Post by requinix »

Well then, what's your actual code?
ouray897
Forum Newbie
Posts: 3
Joined: Wed Mar 07, 2012 11:49 pm

Re: CSV Download File Always Empty

Post by ouray897 »

Problem solved. It had nothing to do with the download code but the DB code. requinix thanks for looking at my code.
Post Reply