简体   繁体   English

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

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

I have a Wordpress theme template file in use by a page. 我有一个页面正在使用的Wordpress主题模板文件。 The template queries the db for an array and then attempts to output the result to a csv file. 该模板在数据库中查询数组,然后尝试将结果输出到csv文件。 No output to the browser is expected. 预期不会输出到浏览器。 Here is code: 这是代码:

/***
 * 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);
}

The file is written to the user's downloads directory as expected, but it is empty. 该文件已按预期写入用户的下载目录,但为空。 I've debugged to verify there is a result of 117 elements. 我已调试以验证是否有117个元素的结果。 The loop above is executing. 上面的循环正在执行。 It's as though the output buffer is not being flushed or is being cleared by Wordpress. 好像输出缓冲区没有被刷新或被Wordpress清除。

Could you try to use : 您可以尝试使用:

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

instead of : 代替 :

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

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

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