I am running a php 8 website on linux web app service in Azure. When the user presses a button I want to create a file on the fly (read data from db) and download it. Instead it is displaying contents on the page. Here is the code
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Type: text/csv; charset=utf-8');
header("Content-Disposition: attachment; filename=geodata.csv");
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize("geodata.csv"));
$file = fopen('php://output', 'w')
or die ("Unable to create file in write mode");
if (ob_get_contents()) ob_end_clean();
fputcsv($output, array('name','loc','zip'), chr(6));
$mrows = mysqli_query($myconnection, "SELECT name,loc,zip FROM geo_tbl WHERE sp = 'FL'");
while ($mrow = mysqli_fetch_assoc($mrows)) {
fputcsv($file, $mrow, chr(6));
}
fclose($file);
readfile("geodata.csv");
exit();
Thanks