I need to export data in one table in a csv file. I\'m able to get the data fine but the CSV file is not being generated by the browser.
My code is like this: its th
I met the same problem. I found that the problem is not the headers code, but the link you click to export. You need to add an argument named "noheader":
Export
I replaced my code with exactly the same headers code mentioned in this question, and it does work as well. My export process code looks like:
if( isset( $_GET['export'] ) ) {
$csv = generate_csv();
$filename = 'results.csv';
$now = gmdate('D, d M Y H:i:s') . ' GMT';
header( 'Content-Type: application/octet-stream' ); // tells browser to download
header( 'Content-Disposition: attachment; filename="' . $filename .'"' );
header( 'Pragma: no-cache' ); // no cache
header( 'Expires: ' . $now ); // expire date
echo $csv;
exit;
}
The last two headers tell the browser not to cache the exported content.