Laravel download base64 encoded files

David Carr

Laravel Framework Tutorials PHP & MySQL

Laravel has the ability to stream files from disc and remove them as soon as they've been downloaded. 

This snippet illustrates this ability. This is useful when you have binanry data that needs storing in order to be emailed and then removed afterwards: 

$attachment = EmailAttachment::where('id', $id)->first();
$path       = public_path($attachment->name);
$contents   = base64_decode($attachment->contentBytes);

//store file temporarily
file_put_contents($path, $contents);

//download file and delete it
return response()->download($path)->deleteFileAfterSend(true);

 

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Fathom Analytics $10 discount on your first invoice using this link

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.