我在尝试创建强制下载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;
答案 0 :(得分:0)
您应该使用Content-Type: application/csv
Content-Type: application/octet-stream
header('Content-Type: application/octet-stream');