How to Download Files From Public Storage Folder in Laravel 8

Last updated on by Digamber

This comprehensive laravel tutorial will share an exorbitantly easy method with you on how to download a file from URL to public storage folder in Laravel 8 application.

Sometimes you get into the situation where you have to create the feature in laravel to download a file from public storage folder; consequently, we will share an intuitive example of laravel download a file from public storage folder.

Here are the process implied in this tutorial, and we would like to discover each step bit by bit. After this, we commence with creating a new laravel app, construct a controller configuration file and write the logic to download a file from the storage folder.

Eventually, we will make a route with two GET methods one display files in laravel view similarly another one download a file from public storage folder.

Install Laravel App

Composer is the foundational tool which is the building block for impeccable laravel development, and it needs to be installed on your machine before starting to work with the laravel platform:

composer create-project laravel/laravel laravel-demo-app --prefer-dist

Ultimately, you can start the app with the php artisan command:

php artisan serve

Set Up Controller

In this step, you have to create a new controller:

php artisan make:controller DownloadFileController

Subsequently, we are going to share a couple of code examples that will help you download files from storage in laravel:

Go ahead and open the app/Http/Controllers/DownloadFileController.php file and, most importantly, update the file with the following code:

The downloadFile() method takes the file name as a parameter, recklessly download files from the public storage folder; after a successful operation, it returns a response with the appropriate content type.

function downloadFile($file_name){
        $file = Storage::disk('public')->get($file_name);
  
        return (new Response($file, 200))
              ->header('Content-Type', 'image/jpeg');
    }

The following code example show files in conjunction with the laravel blade view file:

Furthermore, this example fetches the images from the public storage directory and get the name of these files and send directly to the blade view:

$files = Storage::files("public");

        $imgFiles = array();

        foreach ($files as $key => $val) {
            $val = str_replace("public/","",$val);
            array_push($imgFiles, $val);
        }

    return view('welcome', ['images' => $imgFiles]);

Create Routes

Subsequently, create two routes one to download the file from storage another for showing the downloaded files. Hence open the resources/web.php file and update with the following code:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DownloadFileController;
 
 
Route::get('/', [DownloadFileController::class, 'index']);
Route::get('get/{file_name}', [DownloadFileController::class, 'downloadFile']);

Configure Blade View

Finally, open the resources/views/welcome.blade.php file, add the below code and invoke the laravel view:

@foreach($imgFiles as $img)
 
  <img src="{{route('downloadFile', $img)}}" />
                   
 @endforeach

Conclusion

Laravel download Image file from url to public storage tutorial is completed, i hope you liked this step by step guide.