When I execute the code below, the correct CSV data is in the file in the tmp directory, however the data that is exported is the html code to the web page. No doubt I am making some simple mistake, but I can find it.
$sales->get_all_sales_by_query();
if (count($sales->data_out) > 0) {
$filename = "../tmp/output_file.csv" ;
$fh = fopen($filename, 'w');
$headersOutput = false;
foreach($sales->data_out as $data) {
if(!$headersOutput) {
fputcsv($fh, array_keys($data));
$headersOutput = true;
}
fputcsv($fh, $data);
}
fseek($fh, 0);
// tell the browser it's going to be a csv file
header('Content-Type: application/csv');
// tell the browser we want to save it instead of displaying it
header('Content-Disposition: attachment; filename="MyFile.csv";');
// make php send the generated csv lines to the browser
fpassthru($fh);
fclose($fh);
} else {
$msg = "No records match the criteria";
include "gb_show_messages.php";
}
Todd