首页 > 解决方案 > PHP; 将 CSV 导出到输出缓冲区:加载的文件为空。为什么?

问题描述

文件资源-fopen('php://output')

标题列表:

Content-Type: application/csv
Content-Disposition: attachment; filename="test.csv"
Cache-Control: max-age=0
Expires: 0
Cache-Control: must-revalidate
Pragma: public

我如何下载:

ob_clean();
readfile("php://output");
exit;

readfile 之前的 ob_get_contents() 不为空,并且我也写入文件的数据。

但是加载的文件是空的。为什么

标签: phpheaderhttp-headersexport-to-csvresponse-headers

解决方案


https://www.php.net/manual/en/wrappers.php.php#wrappers.php.output

说:

php://output是一个只写流,允许您以与printecho相同的方式写入输出缓冲区机制。

你正试图从中读取,这显然是做不到的。

我不清楚你实际上想要达到什么目的。


推荐阅读