Skip to content

Commit 40f90f6

Browse files
committed
Fix support for \SplTempFileObject in BinaryFileResponse
1 parent 3d7bbf0 commit 40f90f6

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

BinaryFileResponse.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function setChunkSize(int $chunkSize): static
126126
*/
127127
public function setAutoLastModified(): static
128128
{
129-
$this->setLastModified(\DateTimeImmutable::createFromFormat('U', $this->file->getMTime()));
129+
$this->setLastModified(\DateTimeImmutable::createFromFormat('U', $this->tempFileObject ? time() : $this->file->getMTime()));
130130

131131
return $this;
132132
}
@@ -197,7 +197,9 @@ public function prepare(Request $request): static
197197
$this->offset = 0;
198198
$this->maxlen = -1;
199199

200-
if (false === $fileSize = $this->file->getSize()) {
200+
if ($this->tempFileObject) {
201+
$fileSize = $this->tempFileObject->fstat()['size'];
202+
} elseif (false === $fileSize = $this->file->getSize()) {
201203
return $this;
202204
}
203205
$this->headers->remove('Transfer-Encoding');

Tests/BinaryFileResponseTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,9 @@ public function testCreateFromTemporaryFile()
451451
$this->assertEquals('attachment; filename=temp', $response->headers->get('Content-Disposition'));
452452

453453
ob_start();
454+
$response->setAutoLastModified();
455+
$response->prepare(new Request());
456+
$this->assertSame('7', $response->headers->get('Content-Length'));
454457
$response->sendContent();
455458
$string = ob_get_clean();
456459
$this->assertSame('foo,bar', $string);

0 commit comments

Comments
 (0)