From f5d26b7fe2136ac3e852453d1f5b5e39860f5087 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4rtl?= Date: Mon, 14 Oct 2024 07:27:43 +0200 Subject: [PATCH] Issue #28 Add ignoreUserAbort option to remove files if connection closed --- README.md | 4 ++++ src/File.php | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/README.md b/README.md index 86d7b4a..e4ca13c 100644 --- a/README.md +++ b/README.md @@ -62,3 +62,7 @@ File::$defaultHeader['X-Header'] = 'My Default'; $file = new File('some content', '.html'); $file->send('home.html'); ``` + +The `$ignoreUserAbort` option (on by default) mitigates an issue where the file +was not deleted if the user closes the connection during a download. Try setting +it to `false` if you experience unexpected behavior. diff --git a/src/File.php b/src/File.php index 2271260..10a4ae4 100644 --- a/src/File.php +++ b/src/File.php @@ -19,6 +19,13 @@ class File */ public $delete = true; + /** + * @var bool whether to ignore if a user closed the connection so that the + * temporary file can still be cleaned up in that case. Default is `true`. + * @see https://www.php.net/manual/en/function.ignore-user-abort.php + */ + public $ignoreUserAbort = true; + /** * @var array the list of static default headers to send when `send()` is * called as key/value pairs. @@ -126,6 +133,12 @@ public function send($filename = null, $contentType = null, $inline = false, $he } $this->sendHeaders($headers); + + // #28: File not cleaned up if user aborts connection during download + if ($this->ignoreUserAbort) { + ignore_user_abort(true); + } + readfile($this->_fileName); }