繁体   English   中英

php://输出结果为空文件

[英]php://output results in empty file

我有一个页面正在使用的Wordpress主题模板文件。 该模板在数据库中查询数组,然后尝试将结果输出到csv文件。 预期不会输出到浏览器。 这是代码:

/***
 * Output an array to a CSV file
 */
function download_csv_results($results, $name = NULL)
{
if( ! $name)
{
    $name = md5(uniqid() . microtime(TRUE) . mt_rand()). '.csv';
}

header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='. $name);
header('Pragma: no-cache');
header("Expires: 0");

$outstream = fopen("php://output", "w");

foreach($results as $result)
{
    fputcsv($outstream, $result);
}
fclose($outstream);
}

该文件已按预期写入用户的下载目录,但为空。 我已调试以验证是否有117个元素的结果。 上面的循环正在执行。 好像输出缓冲区没有被刷新或被Wordpress清除。

您可以尝试使用:

$outstream = fopen("php://output", "a");

代替 :

$outstream = fopen("php://output", "w");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM