The PHP documentation for readfile has an example to how to download a file:
<?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;
}
?>
That uses ob_clean to remove the contents that may be in the output buffer.
However I have read posts (http://heap.tumblr.com/post/119127049/a-note-about-phps-output-buffer-and-readfile) that indicate that ob_end_clean should be used instead of ob_clean for large files.
My question is: what is the use of using ob_clean instead of ob_end_clean? If ob_end_clean works as ob_clean does and avoids a problem, why doesn't all documentation show using ob_end_clean instead?
ob_clean()
flushes the buffer, but leaves output buffering active. That means your readfile()
output will be buffered as well.
ob_end_clean()
flushes the buffer, and TURNS OFF buffering completely, allowing readfile()
to dump to the browser directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With