Skip to content

Commit e90984b

Browse files
gladynicolas-grekas
authored andcommitted
[HttpFoundation] Fix memory limit problems in BinaryFileResponse
1 parent 70fd0eb commit e90984b

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

BinaryFileResponse.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class BinaryFileResponse extends Response
3434
protected $offset = 0;
3535
protected $maxlen = -1;
3636
protected $deleteFileAfterSend = false;
37-
protected $chunkSize = 8 * 1024;
37+
protected $chunkSize = 16 * 1024;
3838

3939
/**
4040
* @param \SplFileInfo|string $file The file to stream
@@ -267,7 +267,7 @@ public function prepare(Request $request)
267267
$range = $request->headers->get('Range');
268268

269269
if (str_starts_with($range, 'bytes=')) {
270-
[$start, $end] = explode('-', substr($range, 6), 2) + [0];
270+
[$start, $end] = explode('-', substr($range, 6), 2) + [1 => 0];
271271

272272
$end = ('' === $end) ? $fileSize - 1 : (int) $end;
273273

@@ -341,14 +341,15 @@ public function sendContent()
341341

342342
$length = $this->maxlen;
343343
while ($length && !feof($file)) {
344-
$read = ($length > $this->chunkSize) ? $this->chunkSize : $length;
345-
$length -= $read;
344+
$read = $length > $this->chunkSize || 0 > $length ? $this->chunkSize : $length;
345+
$read = stream_copy_to_stream($file, $out, $read);
346346

347-
stream_copy_to_stream($file, $out, $read);
348-
349-
if (connection_aborted()) {
347+
if (false === $read || connection_aborted()) {
350348
break;
351349
}
350+
if (0 < $length) {
351+
$length -= $read;
352+
}
352353
}
353354

354355
fclose($out);

0 commit comments

Comments
 (0)