-
Notifications
You must be signed in to change notification settings - Fork 17
handle case where user closes browser window before file is sent #28
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
Comments
As you may have seen this library is basically in low maintenance mode for a while mainly because I don't do much PHP development anymore these days. I'm a bit hesitant to do any changes unless I'm very sure it won't break anything. So just few comments:
Reading the manual page for readfile this also looks interesting:
Did you play around with turning output buffering off before sending the file? I can imagine that this may affect how the file is locked. Maybe the output buffer is never flushed properly when the connection is closed and this is why |
Hi Mike, Thanks for your very thoughtful and detailed response. I really appreciate you taking time.
ah - I didn't think of that, and it's a very good point: some of the PDF files we generate are very large - could be a few hundred pages.
the original artifact I was troubleshooting, was a pair of errors (notice the timestamps are identical):
digging into this problem, I was able to reproduce it in my development environment by:
Just before the
no, because I've never been able to catch it.
I assume
not previously, but I just tried this, in
again, if I set a breakpoint on at this point, I have a working solution (as per the ticket description), but I think your comment about memory usage is very important. Given that the error-condition is not very harmful; and the only negative side-effect is a few leftover PDF and HTML files in the temp directory, I'm inclined to keep your original code and just live with the problem. still very puzzled by two things about
I might open a core PHP ticket and see what the PHP gurus have to say about these questions... |
any thoughts? |
Christoph Becker pointed out that calling I'm going to make this change and suggest making a similar change in |
@jjdunn Nice, thanks for the research. I've opened a PR. Could you maybe help testing it? |
Issue #28 Add ignoreUserAbort option to remove files if connection closed
Released 1.3.0 containing this fix. Thanks for bringing this up. |
problem:
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.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" errorAt 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()
: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.
The text was updated successfully, but these errors were encountered: