【发布时间】: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