Black Friday sale is now on! 50% off Laravel: The Modular Way. Learn more

Laravel download base64 encoded files

David Carr

Laravel Framework PHP & MySQL Tutorials

    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 Book by David Carr

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

    Subscribe to my newsletter

    Subscribe and get my books and product announcements.

    © 2009 - 2022 DC Blog. All code MIT license. All rights reserved.