Skip to content

Issue #28 Add ignoreUserAbort option to remove files if connection closed #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
13 changes: 13 additions & 0 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

Expand Down
Loading