【发布时间】:2017-02-07 18:44:03
【问题描述】:
我有以下函数,它从数据库查询中获取一组结果。我在遍历查询结果时填充数组。同时我创建了一个html表。输出 html 表后,我调用函数 download_csv($csvArray) 但没有下载 CSV,它只显示数组的内容,就好像我在数组上做了 var_dump 一样。我可以确认数组的内容是正确的。
注意:这是在外部网页上完成的,而不是来自管理区域。
是否需要调用一些 wordpress 函数才能允许下载,还是我遗漏了什么?
function download_csv($csvArray) {
$file_name = 'report.csv';
//output headers so that the file is downloaded rather than displayed
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=$file_name");
//Disable caching - HTTP 1.1
header("Cache-Control: no-cache, no-store, must-revalidate");
//Disable caching - HTTP 1.0
header("Pragma: no-cache");
//Disable caching - Proxies
header("Expires: 0");
//Start the ouput
$output = fopen("php://output", "w");
//Then loop through the rows
foreach ($csvArray as $fields) {
fputcsv($output, $fields);
}
//Close the stream off
fclose($output);
die();
}
【问题讨论】: