【问题标题】:PHP; Export CSV to output buffer: Loaded File is empty. Why?PHP;将 CSV 导出到输出缓冲区:加载的文件为空。为什么?
【发布时间】:2020-04-29 10:25:40
【问题描述】:

文件资源 - 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;

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

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

【问题讨论】:

    标签: php header http-headers export-to-csv response-headers


    【解决方案1】:

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

    说:

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

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

    我不清楚你真正想要达到的目标。

    【讨论】:

    • 谢谢。它让我有了一些想法。我只是在flush 上复制ob_clean,它是有效的。但我不能绝对承认。如果刷新也删除缓冲区,即使这样 readfile 也能正常工作。我的意思是如果缓冲区下降了,我猜,php://output 应该是空的并且 readfile 不会工作。但事实并非如此,文件正确加载了一些数据。
    猜你喜欢
    • 2014-09-26
    • 1970-01-01
    • 2022-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    • 2016-10-25
    相关资源
    最近更新 更多