Skip to content

Commit

Permalink
Merge branch 'MDL-69355-master-filebuffer' of git://github.com/mudrd8…
Browse files Browse the repository at this point in the history
…mz/moodle into master
  • Loading branch information
sarjona committed Oct 5, 2020
2 parents cc7c3b3 + af30f18 commit 134c45e
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2234,23 +2234,27 @@ function readfile_accel($file, $mimetype, $accelerate) {
}
}

if ($filesize > 10000000) {
// for large files try to flush and close all buffers to conserve memory
while(@ob_get_level()) {
if (!@ob_end_flush()) {
break;
}
}
}

// Send this header after we have flushed the buffers so that if we fail
// later can remove this because it wasn't sent.
header('Content-Length: ' . $filesize);

if (!empty($_SERVER['REQUEST_METHOD']) and $_SERVER['REQUEST_METHOD'] === 'HEAD') {
exit;
}

while (ob_get_level()) {
$handlerstack = ob_list_handlers();
$activehandler = array_pop($handlerstack);
if ($activehandler === 'default output handler') {
// We do not expect any content in the buffer when we are serving files.
$buffercontents = ob_get_clean();
if ($buffercontents !== '') {
error_log('Non-empty default output handler buffer detected while serving the file ' . $file);
}
} else {
// Some handlers such as zlib output compression may have file signature buffered - flush it.
ob_end_flush();
}
}

// send the whole file content
if (is_object($file)) {
$file->readfile();
Expand Down

0 comments on commit 134c45e

Please sign in to comment.