Skip to content

Commit 7a2e3c0

Browse files
committed
Merge branch '4.4' into 5.4
* 4.4: Update ExceptionInterface.php Flush with flush() after ob_end_flush() [HttpFoundation] Fix deleteFileAfterSend on client abortion fix sending request to paths containing multiple slashes
2 parents 91a397a + 1c9739f commit 7a2e3c0

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

BinaryFileResponse.php

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

3839
/**
3940
* @param \SplFileInfo|string $file The file to stream
@@ -125,6 +126,22 @@ public function getFile()
125126
return $this->file;
126127
}
127128

129+
/**
130+
* Sets the response stream chunk size.
131+
*
132+
* @return $this
133+
*/
134+
public function setChunkSize(int $chunkSize): self
135+
{
136+
if ($chunkSize < 1 || $chunkSize > \PHP_INT_MAX) {
137+
throw new \LogicException('The chunk size of a BinaryFileResponse cannot be less than 1 or greater than PHP_INT_MAX.');
138+
}
139+
140+
$this->chunkSize = $chunkSize;
141+
142+
return $this;
143+
}
144+
128145
/**
129146
* Automatically sets the Last-Modified header according the file modification date.
130147
*
@@ -306,7 +323,23 @@ public function sendContent()
306323
$out = fopen('php://output', 'w');
307324
$file = fopen($this->file->getPathname(), 'r');
308325

309-
stream_copy_to_stream($file, $out, $this->maxlen, $this->offset);
326+
ignore_user_abort(true);
327+
328+
if (0 !== $this->offset) {
329+
fseek($file, $this->offset);
330+
}
331+
332+
$length = $this->maxlen;
333+
while ($length && !feof($file)) {
334+
$read = ($length > $this->chunkSize) ? $this->chunkSize : $length;
335+
$length -= $read;
336+
337+
stream_copy_to_stream($file, $out, $read);
338+
339+
if (connection_aborted()) {
340+
break;
341+
}
342+
}
310343

311344
fclose($out);
312345
fclose($file);

Response.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,7 @@ public static function closeOutputBuffers(int $targetLevel, bool $flush): void
12451245
while ($level-- > $targetLevel && ($s = $status[$level]) && (!isset($s['del']) ? !isset($s['flags']) || ($s['flags'] & $flags) === $flags : $s['del'])) {
12461246
if ($flush) {
12471247
ob_end_flush();
1248+
flush();
12481249
} else {
12491250
ob_end_clean();
12501251
}

0 commit comments

Comments
 (0)