I'm trying to export records to csv file after fetch from Mysql
using php
. I'm following this answer instructions and its working fine for me.
function array2csv(array &$array)
{
if (count($array) == 0) {
return null;
}
ob_start();
$df = fopen("php://output", 'w');
fputcsv($df, array_keys(reset($array)));
foreach ($array as $row) {
fputcsv($df, $row);
}
fclose($df);
return ob_get_clean();
}
function download_send_headers($filename) {
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename={$filename}");
// Disable caching
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1
header("Pragma: no-cache"); // HTTP 1.0
header("Expires: 0"); // Proxies
}
download_send_headers("data_export_" . date("Y-m-d") . ".csv");
echo array2csv($s);
die();
But problem is that this function is also adding html materials (Header Content like title and meta title etc
) as well as records, so can anyone guide me how can i prevent to add header content for csv file. I will appreciate if someone guide me.