From 5281eec6c31746638a6da34dc713ae68babd220e Mon Sep 17 00:00:00 2001 From: Giuseppe Criscione <18699708+giuscris@users.noreply.github.com> Date: Sun, 6 Sep 2026 22:32:53 +0200 Subject: [PATCH] Add the possibility to delete file responses after send and set the download filename --- formwork/src/Http/FileResponse.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/formwork/src/Http/FileResponse.php b/formwork/src/Http/FileResponse.php index f54c210e6..7a5db5b0b 100644 --- a/formwork/src/Http/FileResponse.php +++ b/formwork/src/Http/FileResponse.php @@ -4,6 +4,7 @@ use Formwork\Http\Utils\Header; use Formwork\Utils\FileSystem; +use LogicException; use RuntimeException; class FileResponse extends Response @@ -32,9 +33,10 @@ public function __construct( protected string $path, ResponseStatus $responseStatus = ResponseStatus::OK, array $headers = [], - bool $download = false, + protected bool $download = false, protected bool $autoEtag = false, protected bool $autoLastModified = false, + protected bool $deleteAfterSend = false, ) { $this->fileSize = FileSystem::fileSize($path); @@ -101,6 +103,10 @@ public function send(): void } $this->flush(); + + if ($this->deleteAfterSend) { + unlink($this->path); + } } public function prepare(Request $request): static @@ -160,4 +166,18 @@ public function prepare(Request $request): static return $this; } + + /** + * Set filename for download + * + * @throws LogicException If the response is not a download response + */ + public function setFilename(string $filename): static + { + if (!$this->download) { + throw new LogicException('Cannot set filename for inline file response'); + } + $this->headers->set('Content-Disposition', Header::make(['attachment', 'filename' => $filename])); + return $this; + } }