Skip to content
Advertisement

Deleting files after download in laravel

I’m creating a application that lets a user download a file. After the download i want the file to be deleted. The end of my code is like this:

JavaScript

which means that the function ends on the return an i am not able to delete the file. I have tried to call a ‘after’ filter on the route but then the file gets deleted to quickly.

Any ideas?

Advertisement

Answer

I personally use the following;

JavaScript

Status code is obviously the code which you want to return.

Within the $header parameter you can pass an array with the indexes Content-Type and Content-Disposition.

Then you can simply unlink $path_to_file and return $response.


Much easier way of deleting a file would be to use Jon’s answer for versions of Laravel > 4.0.

You can use deleteFileAfterSend http://laravel.com/docs/5.0/responses#other-responses

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement