Closed
Description
problem:
- user requests a file to be downloaded
- file-download-link (or button) opens a new tab for the file-download target
- user closes the new tab before the file is downloaded
tmp/File::send()
doesreadfile()
- but this fails since the browser connection has been closedreadfile()
throws an uncatchable Exception at this point - the only place it can be handled is the shutdown function. I tried various try/catch blocks aroundreadfile()
, none of which got invoked.- after the shutdown function executes,
tmp::__destruct()
runs, and this fails because the file is locked by the web server, left over fromreadfile()
unlink()
in__destruct()
throws a "permission denied" error
At first I wrote a (working) solution in my shutdown function, which checks for leftover open file resources of type "stream", and unlocks them; but this solution runs on every request so it's very high overhead to solve an edge-case error
solution I like better, in tmp/File::send()
:
//readfile($this->_fileName);
$fpointer = fopen($this->_fileName, 'r');
$content = fread($fpointer, filesize($this->_fileName));
fclose($fpointer);
echo $content;
this works exactly the same as readfile()
under normal conditions, but if the user closes the browser window, the temp files can be deleted since the lock has been released.
p.s. all of this on Windows/Apache httpd 2.4.62 with PHP 8.3.11. Maybe it would work differently on Linux or a different web server.
Metadata
Metadata
Assignees
Labels
No labels