Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rmiller335's avatar

Remove download file if download canceled

I'm generating a zip file and downloading it in a controller. I do this:

        App::finish(function($request, $response) use ($archFile) {
            unlink($archFile);
        });

before I return the Response::download, and it works fine as long as User downloads the file. But if User hits Cancel the file doesn't get removed. Is there some other event I can listen to in this case?

0 likes
5 replies
rmiller335's avatar

That sounds like what I was looking for in the first place. I'll check it out, thanks.

rmiller335's avatar

Well, unfortunately I can't get that package to work in Laravel. It keeps complaining that it can't find gzdeflate, despite the fact that it's installed and I have a test program that uses it from the command line and that works. At first it said it couldn't find \ZipStream\gzdeflate, so I spent an unhappy few hours trying to figure out how to set up a facade for ZipStream. Then I tried just changing it to \gzdeflate in the zipstream source and it says it can't find that either. So clearly there's something fundamental I'm missing here, and perhaps someone could point me to a source of enlightenment. Meanwhile, my original request stands, in order to get the thing working without leaving files around.

nolros's avatar

I assume no params to set a temp path or file name? What about using a decorator to add a suffix to the temp file name e.g. user id then when cancel look for anything with user id and delete ?

DivDax's avatar

If you're using Homestead it should work out of the box. I never had a problem or installed the required mod. To work with temp files why not empty the folder every hour or at midnight? Of course only files older than 1 hour or so?

Please or to participate in this conversation.