Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Laravel 5 – Download file from Laravel storage

An example for retrieving a file from the storage folder of Laravel. (Often you do not want files being saved in a public/upload folder for obvious reasons). For example, you would add this function to your routes.php, and when you get/3.png the browser will download the file with filename of 3.png. Filenames should be unique, it’s also a good idea to keep the original filename in the database along with the new unique one that was created at upload.

public function get($filename){

        $entry = Fileentry::where('filename', '=', $filename)->firstOrFail();
        $file = Storage::disk('local')->get($entry->filename);
        $storagePath  = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();
        $filename = $storagePath . "/" . $entry->filename;
        header('Content-Type: application/octet-stream');
        header("Content-Transfer-Encoding: Binary");
        header("Content-disposition: attachment; filename=\"" . basename($entry->filename) . "\"");
        readfile ($filename);
    }

 



This post first appeared on Ninjamedia Web Development, please read the originial post: here

Share the post

Laravel 5 – Download file from Laravel storage

×

Subscribe to Ninjamedia Web Development

Get updates delivered right to your inbox!

Thank you for your subscription

×