I created a page that downloads a .csv file recently.
I have a html file that takes in 2 dates that will do a post using slim. The php code will then create a .csv file and populate it based on the data returned form a mysqli query. I have tested this in chrome, firefox, and safari.
Code;
$app->post('/api/dateRangeReport',function ($request, $response, $args) {
$jsonArgs = $request->getParsedBody();
header('Content-type: text/csv');
$name = $jsonArgs['startDate']." through ".$jsonArgs['endDate'].".csv";
header('Content-Disposition: attachment; filename="'.$name.'"');
// do not cache the file
header('Pragma: no-cache');
header('Expires: 0');
// create a file pointer connected to the output stream
$file = fopen('php://output', 'w');
// send the column headers
fputcsv($file, array('tracking_number', 'store', 'street', 'state', 'zip', 'city', 'country', 'date', 'serial_number'));
global $fawkesEngine;
$results = $fawkesEngine->createDateRangeReport($jsonArgs);
$data = array();
// need to do some special formatting to prevent scientific notation
foreach ($results as $value) {
$formattedTrackingNumber = $value['tracking_number'];
$formattedTrackingNumber = "=".'"'.$formattedTrackingNumber.'"';
$formattedSerialNumber = $value['serial_number'];
$formattedSerialNumber = "=".'"'.$formattedSerialNumber.'"';
$dataTemp = array($formattedTrackingNumber,$value['store'],$value['street'],$value['state'],$value['zip'],$value['city']
,$value['country'],$value['received_date'],$formattedSerialNumber);
array_push($data, $dataTemp);
}
// output each row of the data
foreach ($data as $row)
{
fputcsv($file, $row);
}
fclose($file);
exit;
});