【发布时间】:2016-10-17 00:25:12
【问题描述】:
我正在尝试通过 php 导出和下载 csv 文件。我已经完全按照Export to CSV via PHP
中的建议做了我可以在响应中看到我的数组转储,但 csv 文件只是没有下载。请帮忙。
这是我的代码:
function download_send_headers($filename) {
// disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// force download
Header('Content-Description: File Transfer');
header("Content-Type: application/force-download");
// header("Content-Type: application/octet-stream");
// header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
function array2csv(array &$array)
{
// if (count($array) == 0) {
// return null;
// }
ob_start();
$df = fopen("php://output", 'w');
fputcsv($df, array_keys(reset($array)));
foreach ($array as $row) {
fputcsv($df, $row);
}
fclose($df);
return ob_get_clean();
}
我是这样使用它的:
download_send_headers("data_export_" . date("Y-m-d") . ".csv");
echo array2csv($modsucc);
die();
【问题讨论】:
-
发布您的代码。
-
远程调试已经够难的了,但是当我们看不到代码的时候就几乎不可能了
-
我已添加代码..请查看
-
检查您的输出缓冲是否“开启”。请检查:ini_get('output_buffering');更多详情请查看:stackoverflow.com/questions/5608975/…
-
输出缓冲已启用,我已经检查过了。它是否取决于输出缓冲的缓冲区大小?..目前它的默认值是 4kb