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删除输出缓冲区中可能包含的内容。
然而,我读到了posts (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呢?
发布于 2014-01-21 15:28:21
ob_clean()刷新缓冲区,但使输出缓冲处于活动状态。这意味着您的readfile()输出也将被缓冲。
ob_end_clean()刷新缓冲区,并完全关闭缓冲区,允许readfile()直接转储到浏览器。
https://stackoverflow.com/questions/21262271
复制相似问题