【问题标题】:Change from ob_clean to ob_end_clean?从 ob_clean 更改为 ob_end_clean?
【发布时间】:2014-01-21 15:26:03
【问题描述】:

readfile 的 PHP 文档有一个如何下载文件的示例:

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?> 

这使用 ob_clean 来删除可能在输出缓冲区中的内容。

但是我读过帖子 (http://heap.tumblr.com/post/119127049/a-note-about-phps-output-buffer-and-readfile) 表明对于大文件应该使用 ob_end_clean 而不是 ob_clean。

我的问题是:使用 ob_clean 代替 ob_end_clean 有什么用?如果 ob_end_clean 像 ob_clean 一样工作并且避免了问题,为什么不是所有文档都显示使用 ob_end_clean 来代替?

【问题讨论】:

  • 它们之间的区别是ob_clean 擦除缓冲区然后继续缓冲,而ob_end_clean 擦除它然后停止缓冲。

标签: php


【解决方案1】:

ob_clean() 刷新缓冲区,但保持输出缓冲处于活动状态。这意味着您的 readfile() 输出也将被缓冲。

ob_end_clean() 刷新缓冲区,并完全关闭缓冲,允许readfile() 直接转储到浏览器。

【讨论】:

  • 感谢您指出 readfile() 也会被缓冲。这就是我错过的重点。
  • @MarcB 为什么flush()ob_clean() 之后? ob_clean 不应该是 flushing 缓冲区吗?
猜你喜欢
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-01
  • 1970-01-01
  • 2023-03-23
相关资源
最近更新 更多