33

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:

return Response::download(public_path() . '/uploads/_tmp/' . urldecode($filename));

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?

1
  • Do you mean you want the temporary file that PHP creates to be deleted, or the file that you have just download and put somewhere on your file system. Because the temporary file created and used by PHP in the download process is deleted by PHP automatically.
    – RiggsFolly
    Oct 10, 2014 at 9:19

7 Answers 7

65

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

return response()->download($filePath, $fileName, $headers)->deleteFileAfterSend(true);

5
  • 1
    See @cbloss793 comment below "This works in Laravel 4.2. Just place it in the controller instead of the app/start/global.php."
    – Jon
    Nov 10, 2015 at 5:41
  • DeleteFileAfterSend(true) works great on Laravel 5.3 as well. Although the documentation doesn't state anything about it, you can still use it. Maybe it will be deprecated anytime soon.
    – NielsKoole
    Nov 8, 2016 at 14:08
  • 2
    Note that deleteFileAfterSend(true) is actually a Symfony feature (part of HttpFoundation), which appears to have been added in 2014.
    – Jonathan
    Dec 23, 2016 at 13:42
  • Nice! This even works when streaming a file, i.e. return response()->file(...)->deleteFileAfterSend(true); Mar 11, 2021 at 23:55
  • Thanks a lot! 7 years later and you still helped me! I wonder why this was removed from the Laravel docs?!
    – mniess
    Feb 18, 2022 at 12:17
50

I personally use the following;

$response = Response::make(file_get_contents($path_to_file), $status_code, $headers);

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

return response()->download($filePath, $fileName, $headers)->deleteFileAfterSend(true);
1
  • Worked for me. Thanks a lot!
    – Deepak
    Dec 30, 2020 at 5:49
9

Simply use this line of code:

return response()->download($file_path)->deleteFileAfterSend(true);

Here, inside download function the file path will be passed as an argument. Let's say for an example you want to backup your database in a file and also want to delete with downloading:

$date = Carbon::now()->format('Y-m-d_h-i');
$pub_path = public_path();
$file_path = $pub_path . '/application/db_backups/' . $date . '.sql';
$output = shell_exec('mysqldump -h58.84.34.65 -uwsit -pwsit97480 websms > ' . $file_path);
return response()->download($file_path)->deleteFileAfterSend(true);
1
  • 2
    Code only answers are not always easy to understand to others. It would be great if you could add a short comment on how your proposal solves the OP's problem Jan 27, 2020 at 4:50
9

If you want to delete the source file after downloading, simply write it as follows

return response()->download($filePath)->deleteFileAfterSend(true);
5
$filename = storage_path() . '/testing.txt';

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

return Response::download($filename);
4
  • 2
    Try explaining your answer.
    – Sleek Geek
    Apr 8, 2015 at 19:29
  • 2
    This works in Laravel 4.2. Just place it int he controller instead of the app/start/global.php. Brilliant!
    – cbloss793
    Oct 26, 2015 at 19:15
  • Don't use if you have laravel > 5.0
    – CommandZ
    Apr 19, 2016 at 18:28
  • 1
    I second @CommandZ, use Jon's method above for any version 5 and above.
    – cbloss793
    Sep 26, 2016 at 22:29
0

For Laravel 5.8 use stream download. In the callback function, delete the file after echo the contents.

Let's take a look at the solution:


return response()->streamDownload(function () use ($pathToTheFile) {
    echo Storage::get($pathToTheFile);
    Storage::delete($pathToTheFile);
}, 'my-awesome-file-name');

I don't know if it works for the oldest or the latest versions.

-8
unlink('./path/filename.extension');
2
  • it would be nice if you could add a little explanation to your answer, not just a one-liner
    – GNi33
    Oct 10, 2014 at 10:10
  • In the question he returns the response. This code would never even run.
    – Mattias
    Jan 30, 2015 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.