Page 1 of 1

CSV Download File Always Empty

Posted: Thu Mar 08, 2012 12:10 am
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);


Re: CSV Download File Always Empty

Posted: Thu Mar 08, 2012 2:19 pm
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.

Re: CSV Download File Always Empty

Posted: Thu Mar 08, 2012 2:21 pm
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".

Re: CSV Download File Always Empty

Posted: Thu Mar 08, 2012 5:38 pm
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.

Re: CSV Download File Always Empty

Posted: Thu Mar 08, 2012 7:15 pm
by requinix
Well then, what's your actual code?

Re: CSV Download File Always Empty

Posted: Thu Mar 08, 2012 11:17 pm
by ouray897
Problem solved. It had nothing to do with the download code but the DB code. requinix thanks for looking at my code.