【问题标题】:Ob_start not catching output fileOb_start 没有捕获输出文件
【发布时间】:2017-04-20 10:42:38
【问题描述】:

我正在尝试生成报告并上传。问题是当生成文件的函数以 readfile() 结尾时,即使我在函数调用之前放置了ob_start(),它也只会下​​载文件。所以我不明白为什么ob_start 没有抓住它。

问题应该在这里

  1. 我打电话给ob_start()

  2. 然后我调用像这样输出文件的函数

    header('Content-Type: application/vnd.openxmlformats-officedocument.' . 'wordprocessingml.document'); header('Content-Disposition: attachment; filename="' . $fileNameDownload . '"'); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . filesize($fileName . '.' . $this->_extension)); readfile($fileName . '.' . $this->_extension);

  3. 在之前的函数调用之后我把 $content = ob_get_contents();

和 4.ob_end_clen()

我希望 readfile() 函数将开始写入缓冲区,但它会输出文件以供下载

【问题讨论】:

标签: php phpdocx


【解决方案1】:

我相信标题正在设置,而不是被输出缓冲区捕获,您可能需要取消它们,特别是因为Content-Disposition: attachment 将响应设置为强制下载。

http://www.php.net/manual/en/function.ob-start.php

此函数将打开输出缓冲。当输出缓冲处于活动状态时,脚本(除了标题)不会发送任何输出,而是将输出存储在内部缓冲区中。

因此如果函数设置:

header('Content-Type: application/vnd.openxmlformats-officedocument.' . 'wordprocessingml.document');
header('Content-Disposition: attachment; filename="' . $fileNameDownload . '"');

您需要在结束输出缓冲捕获后取消它,即:

header('Content-Type: text/html');
header('Content-Disposition: inline');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    相关资源
    最近更新 更多