Skip to content

Commit 4f4d5a2

Browse files
bug #58712 [HttpFoundation] Fix support for \SplTempFileObject in BinaryFileResponse (elementaire)
This PR was merged into the 7.1 branch. Discussion ---------- [HttpFoundation] Fix support for `\SplTempFileObject` in `BinaryFileResponse` | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | yes | PR original Feature | #49144 | New feature? | no | Deprecations? | no | Issues | - | License | MIT We can not call some methods with an object of `\SplTempFileObject()`. We get this error: `[...] stat failed for php://temp`. I have checked the code against methods listed in [this note](https://www.php.net/manual/en/class.spltempfileobject.php#128962). I have found: - `getMTime()` called in `BinaryFileResponse::setAutoLastModified()` - `getSize()` called in `BinaryFileResponse::prepare()` - `isReadable()` called in `BinaryFileResponse::setFile()` (already safe) I have updated the unit test and patched the class `BinaryFileResponse`. Note: calling `SplFileObject::fstat()` gives `mtime` equals to `0`. I think it is nonsense to use that as value for last modified. I have decided to use `time()` because i guess, we can not do better. Indeed, we have no idea how much time have passed between making the temp file and the call to `setAutoLastModified()` by the developper. Commits ------- a104d50cb7a Fix support for \SplTempFileObject in BinaryFileResponse
2 parents 6472a9f + 40f90f6 commit 4f4d5a2

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

BinaryFileResponse.php

+4-2
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

+3
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)