Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merged in readfile_chunked() patch -- much lower memory footprint, sa…

…me CPU usage, works around PHP5 bug in readfile()
  • Loading branch information...
commit 9fd70b1ac32cb8d28424c299cecd04754a14b856 1 parent 6eabcba
martinlanghoff authored
Showing with 24 additions and 2 deletions.
  1. +24 −2 lib/filelib.php
View
26 lib/filelib.php
@@ -167,7 +167,7 @@ function send_file($path, $filename, $lifetime=86400 , $filter=false, $pathisstr
if ($pathisstring) {
echo $path;
}else {
- readfile($path);
+ readfile_chunked($path);
}
} else { // Try to put the file through filters
global $course; // HACK!
@@ -199,7 +199,7 @@ function send_file($path, $filename, $lifetime=86400 , $filter=false, $pathisstr
if ($pathisstring) {
echo $path;
}else {
- readfile($path);
+ readfile_chunked($path);
}
}
}
@@ -344,5 +344,27 @@ function fulldelete($location) {
return true;
}
+function readfile_chunked($filename,$retbytes=true) {
+ $chunksize = 1*(1024*1024); // 1MB chunks
+ $buffer = '';
+ $cnt =0;// $handle = fopen($filename, 'rb');
+ $handle = fopen($filename, 'rb');
+ if ($handle === false) {
+ return false;
+ }
+
+ while (!feof($handle)) {
+ $buffer = fread($handle, $chunksize);
+ echo $buffer;
+ if ($retbytes) {
+ $cnt += strlen($buffer);}
+ }
+ $status = fclose($handle);
+ if ($retbytes && $status) {
+ return $cnt; // return num. bytes delivered like readfile() does.
+ }
+ return $status;
+}
+
?>
Please sign in to comment.
Something went wrong with that request. Please try again.