Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change from ob_clean to ob_end_clean?

Tags:

php

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?

like image 230
useSticks Avatar asked Jan 21 '14 15:01

useSticks


1 Answers

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.

like image 123
Marc B Avatar answered Nov 14 '22 21:11

Marc B