Skip to content

Commit d9afb27

Browse files
bug #48972 [HttpFoundation] Fix memory limit problems in BinaryFileResponse (glady)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [HttpFoundation] Fix memory limit problems in BinaryFileResponse see #48692 | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #48692 | License | MIT | Doc PR | - Commits ------- 8a9949521f [HttpFoundation] Fix memory limit problems in BinaryFileResponse
2 parents 05cd1ac + e90984b commit d9afb27

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)