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

amir5's avatar
Level 7

Laravel do some action after user finishes downloading file

Currently I do this to send file to user:

return response()->download($path);
// or
return response()->file($path);

but how can I do some actions after user finishes downloading the file? Does backend sends the whole file at once to user, and so I can't check when user will completely download it? or the backend sends files with speed of user's internet speed?

0 likes
4 replies
krisi_gjika's avatar

what do you want to do after that you cannot do before?

jj15's avatar

response()->download() sends the entire file to the user's browser, instructing them to download it. Depending on their browser settings, the user may be prompted before saving.

response()->file() sends the entire file to the user's browser, instructing them to display it. Such as an image or PDF that is displayed within the browser itself.

Ideally, you'd perform any needed actions before the response is returned. You could use the defer() helper, which accepts a closure to perform after the response has been sent.

But keep in mind that anything you do inside of defer() will need to take less than the max execution time set in your php.ini (and minus the time it took the response itself).

amir5's avatar
Level 7

@jj15 Does when user clicks save button, the defer function gets executed or when user completely downloads it? (maybe it takes the user 30 minutes to download that file)

jj15's avatar
jj15
Best Answer
Level 10

@amir5 Laravel generates a response with the contents of the file and the correct headers to force a download, hands that off to the web server to handle the actual transfer, and then terminates, executing any closures defined with the defer() helper.

You won't really know if the user has successfully downloaded the file or not, they could abort it themselves halfway through. You'll have to assume that if they've clicked the button and the response was successfully sent, they've technically downloaded the file.

1 like

Please or to participate in this conversation.