Search code examples
phpdownloadlaravel-5.1response

Laravel - Delete local file after served to client


I am using the following to serve a PDF file to a user return

response()->download($filename); 

Once the file has been served for download, there is no need to keep it on the server and I would like to remove it so they don't clog the server storage. What is the best way to remove the file after it has been downloaded? I was going to use terminable middleware to handle it. Is that the best way to do it with Laravel 5.1?


Solution

  • Just to post this here in case anyone else has this question...

    It is possible to flash the file name to the session and then use terminable middleware to fetch the session variable and unlink the file.