我在Azure的Linux Web应用程序服务上运行一个PHP 8网站。当用户按下按钮时,我希望动态生成一个文件(从数据库读取数据)并将其下载。但实际上它会在页面上显示文件内容。以下是代码:
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();
谢谢
$myconnection
从未设置,mysqli_query()
可能无法工作。 - RiggsFolly