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

atouzel's avatar

Can we send back a cookie with a file response?

Users can download a dynamically generated file by clicking on a link tag that routes to a controller which respond with:

return $response->file(base_path('storage/app/...'));

I want to send a cookie back with the file (its purpose is to be detected by my React app so it knows the file has been downloaded). I tried to simply do:

return $response->file(base_path('storage/app/...'))->cookie('downloadConfirm', '12345', 60);

Then, when clicking on the link, the file tries to download but it fails due to a 'problem with the server'.

Can we actually send back cookies with files?

0 likes
1 reply
bobbybouwmann's avatar

The file method is given you back a BinaryFileResponse object. You can't call cookie on that, and that's why you get the server error. It's trying to call a method that doesn't exist.

There is another way and that's just setting a session for example in your application or update a database row and do a request to that after that. So you call an url to check if the download has been done or not. However you don't know when or if the download was successful that way. Note that you also don't know that with a cookie of course.

Please or to participate in this conversation.