diff --git a/BinaryFileResponse.php b/BinaryFileResponse.php index 5bc8b0e16..a2b160f8a 100644 --- a/BinaryFileResponse.php +++ b/BinaryFileResponse.php @@ -126,7 +126,7 @@ public function setChunkSize(int $chunkSize): static */ public function setAutoLastModified(): static { - $this->setLastModified(\DateTimeImmutable::createFromFormat('U', $this->file->getMTime())); + $this->setLastModified(\DateTimeImmutable::createFromFormat('U', $this->tempFileObject ? time() : $this->file->getMTime())); return $this; } @@ -197,7 +197,9 @@ public function prepare(Request $request): static $this->offset = 0; $this->maxlen = -1; - if (false === $fileSize = $this->file->getSize()) { + if ($this->tempFileObject) { + $fileSize = $this->tempFileObject->fstat()['size']; + } elseif (false === $fileSize = $this->file->getSize()) { return $this; } $this->headers->remove('Transfer-Encoding'); diff --git a/Tests/BinaryFileResponseTest.php b/Tests/BinaryFileResponseTest.php index b58276cf7..77bc32e8c 100644 --- a/Tests/BinaryFileResponseTest.php +++ b/Tests/BinaryFileResponseTest.php @@ -451,6 +451,9 @@ public function testCreateFromTemporaryFile() $this->assertEquals('attachment; filename=temp', $response->headers->get('Content-Disposition')); ob_start(); + $response->setAutoLastModified(); + $response->prepare(new Request()); + $this->assertSame('7', $response->headers->get('Content-Length')); $response->sendContent(); $string = ob_get_clean(); $this->assertSame('foo,bar', $string);