Skip to content

Commit a5392be

Browse files
authored
Merge pull request #29 from mikehaertl/28-ignore-user-abort
Issue #28 Add ignoreUserAbort option to remove files if connection closed
2 parents 70a5b70 + f5d26b7 commit a5392be

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,7 @@ File::$defaultHeader['X-Header'] = 'My Default';
6262
$file = new File('some content', '.html');
6363
$file->send('home.html');
6464
```
65+
66+
The `$ignoreUserAbort` option (on by default) mitigates an issue where the file
67+
was not deleted if the user closes the connection during a download. Try setting
68+
it to `false` if you experience unexpected behavior.

src/File.php

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ class File
1919
*/
2020
public $delete = true;
2121

22+
/**
23+
* @var bool whether to ignore if a user closed the connection so that the
24+
* temporary file can still be cleaned up in that case. Default is `true`.
25+
* @see https://www.php.net/manual/en/function.ignore-user-abort.php
26+
*/
27+
public $ignoreUserAbort = true;
28+
2229
/**
2330
* @var array the list of static default headers to send when `send()` is
2431
* called as key/value pairs.
@@ -126,6 +133,12 @@ public function send($filename = null, $contentType = null, $inline = false, $he
126133
}
127134

128135
$this->sendHeaders($headers);
136+
137+
// #28: File not cleaned up if user aborts connection during download
138+
if ($this->ignoreUserAbort) {
139+
ignore_user_abort(true);
140+
}
141+
129142
readfile($this->_fileName);
130143
}
131144

0 commit comments

Comments
 (0)