Skip to content

Commit

Permalink
handle large files download (PHP)
Browse files Browse the repository at this point in the history
  • Loading branch information
psolom committed Oct 30, 2016
1 parent 4c160bc commit de5bbf7
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions connectors/php/LocalFilemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -836,18 +836,30 @@ public function actionDownload()
$this->error($this->lang('ERROR_CREATING_ZIP'));
}
}
$file_size = $this->get_real_filesize($target_fullpath);

header('Content-Description: File Transfer');
header('Content-Type: ' . mime_content_type($target_fullpath));
header('Content-Disposition: attachment; filename=' . basename($target_fullpath));
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . $this->get_real_filesize($target_fullpath));
header('Content-Length: ' . $file_size);
// handle caching
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

readfile($target_fullpath);
$chunk_size = 16 * 1024 * 1024;
// read file by chunks to handle large files
if ($chunk_size && $file_size > $chunk_size) {
$handle = fopen($target_fullpath, 'rb');
while (!feof($handle)) {
echo fread($handle, $chunk_size);
@ob_flush();
@flush();
}
fclose($handle);
}

Log::info('downloaded "' . $target_fullpath . '"');
exit;
}
Expand Down

0 comments on commit de5bbf7

Please sign in to comment.