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