diff --git a/lib/filelib.php b/lib/filelib.php index 3dcebbf5f74..c8e98f503dd 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -2219,20 +2219,20 @@ function readfile_accel($file, $mimetype, $accelerate) { if ($ranges) { if (is_object($file)) { $handle = $file->get_content_file_handle(); + if ($handle === false) { + throw new file_exception('storedfilecannotreadfile', $file->get_filename()); + } } else { $handle = fopen($file, 'rb'); + if ($handle === false) { + throw new file_exception('cannotopenfile', $file); + } } byteserving_send_file($handle, $mimetype, $ranges, $filesize); } } } - header('Content-Length: '.$filesize); - - if (!empty($_SERVER['REQUEST_METHOD']) and $_SERVER['REQUEST_METHOD'] === 'HEAD') { - exit; - } - if ($filesize > 10000000) { // for large files try to flush and close all buffers to conserve memory while(@ob_get_level()) { @@ -2242,11 +2242,21 @@ function readfile_accel($file, $mimetype, $accelerate) { } } + // 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; + } + // send the whole file content if (is_object($file)) { $file->readfile(); } else { - readfile_allow_large($file, $filesize); + if (readfile_allow_large($file, $filesize) === false) { + throw new file_exception('cannotopenfile', $file); + } } } diff --git a/lib/filestorage/file_system.php b/lib/filestorage/file_system.php index 8cf9ffcdc6d..50156960d78 100644 --- a/lib/filestorage/file_system.php +++ b/lib/filestorage/file_system.php @@ -63,7 +63,9 @@ abstract class file_system { } else { $path = $this->get_remote_path_from_storedfile($file); } - readfile_allow_large($path, $file->get_filesize()); + if (readfile_allow_large($path, $file->get_filesize()) === false) { + throw new file_exception('storedfilecannotreadfile', $file->get_filename()); + } } /** diff --git a/lib/setuplib.php b/lib/setuplib.php index f291a6dfc1f..4aa3b9ad136 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -362,6 +362,10 @@ function default_exception_handler($ex) { $info = get_exception_info($ex); + // If we already tried to send the header remove it, the content length + // should be either empty or the length of the error page. + @header_remove('Content-Length'); + if (is_early_init($info->backtrace)) { echo bootstrap_renderer::early_error($info->message, $info->moreinfourl, $info->link, $info->backtrace, $info->debuginfo, $info->errorcode); } else {