Skip to content

Commit 8d3a5b6

Browse files
committed
Read files in chunks to avoid memory limits when download_via_php is enabled. Fixes blueimp#2334.
1 parent 9ffc36d commit 8d3a5b6

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

server/php/UploadHandler.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/*
3-
* jQuery File Upload Plugin PHP Class 6.4.5
3+
* jQuery File Upload Plugin PHP Class 6.4.6
44
* https://github.com/blueimp/jQuery-File-Upload
55
*
66
* Copyright 2010, Sebastian Tschan
@@ -63,6 +63,9 @@ function __construct($options = null, $initialize = true, $error_messages = null
6363
),
6464
// Enable to provide file downloads via GET requests to the PHP script:
6565
'download_via_php' => false,
66+
// Read files in chunks to avoid memory limits when download_via_php
67+
// is enabled, set to 0 to disable chunked reading of files:
68+
'readfile_chunk_size' => 10 * 1024 * 1024, // 10 MiB
6669
// Defines which files can be displayed inline when downloaded:
6770
'inline_file_types' => '/\.(gif|jpe?g|png)$/i',
6871
// Defines which files (based on their names) are accepted for upload:
@@ -628,6 +631,18 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
628631
}
629632

630633
protected function readfile($file_path) {
634+
$file_size = $this->get_file_size($file_path);
635+
$chunk_size = $this->options['readfile_chunk_size'];
636+
if ($chunk_size && $file_size > $chunk_size) {
637+
$handle = fopen($file_path, 'rb');
638+
while (!feof($handle)) {
639+
echo fread($handle, $chunk_size);
640+
ob_flush();
641+
flush();
642+
}
643+
fclose($handle);
644+
return $file_size;
645+
}
631646
return readfile($file_path);
632647
}
633648

0 commit comments

Comments
 (0)