diff --git a/BinaryFileResponse.php b/BinaryFileResponse.php index 9c6b611e3..6eddd6c28 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 bad3329d2..2951cdb51 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);