我在尝试创建强制下载 csv 文件时遇到问题。相反,数据被打印在屏幕上。我已将此 Excel 导出脚本保存在单独的 php 文件中,并将其包含在另一个 php 文件中。
这是我的代码:
$query = "SELECT * FROM ".$caseStudyTable;
$result = mysql_query($query);
if(!$result)
{
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
// output headers so that the file is downloaded rather than displayed
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=export.csv");
header("Pragma: no-cache");
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
mysql_close($connect);
// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($result)) fputcsv($output, $row);
exit;
最佳答案
而不是使用 Content-Type: application/csv
你应该使用Content-Type: application/octet-stream
header('Content-Type: application/octet-stream');
关于php - MySQL php查询打印到屏幕而不是创建强制下载csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41309783/