In this tutorial you will learn about the Laravel 8 Download File From URL to Public Storage Folder and its application with practical example.
In this laravel download file from public storage folder example, I’ll show you how to download or display files from public storage folder in laravel 8 application. In this tutorial you will learn how to download or display files from public storage folder in laravel 8. In this article I will share example to display and download file from public storage folder in laravel 8.
Laravel 8 Download File From URL to Public Storage Folder
In this step by step tutorial I’ll guide you through the step to download files from public storage folder. And as well as display files on laravel blade views:
Steps 1: Routes
In this step, you need to add the following routes on web.php file. Go to routes directory and open web.php file then update the routes as following:
web.php
1 2 3 4 5 |
use App\Http\Controllers\FileController; Route::get('view', [FileController::class, 'index']); Route::get('get/{filename}', [FileController::class, 'getfile']); |
Step 2: Create Controller File
Now, go to app/controllers and create a controller file named FileController.php. Then update put the following methods in it:
1 2 3 4 5 6 |
function getFile($filename){ $file=Storage::disk('public')->get($filename); return (new Response($file, 200)) ->header('Content-Type', 'image/jpeg'); } |
The above code will download files from public storage by giving the file name and return a response with correct content type. To display files on blade views, so you have to update the following methods into your controller file:
1 2 3 4 5 6 7 |
$files = Storage::files("public"); $images=array(); foreach ($files as $key => $value) { $value= str_replace("public/","",$value); array_push($images,$value); } return view('show', ['images' => $images]); |
The above lined of code fetches the image files from the public storage folder and extract the name of these files and you pass them to your view.
Step 3: Create Blade View
Now, go to resources\view
folder and create a blade view file named show.blade.php. Then update the following code into it:
1 2 3 4 5 6 7 8 |
@foreach($images as $image) <div> <img src="{{route('getfile', $image)}}" class="img-responsive" /> </div> @endforeach |
In Laravel 5,6,7,8, you can do this for a local file to download it:
1 |
return response()->download('path/to/file/image.jpg'); |
You can download external image using
copy()
function, then send it to user in the response:
1 2 3 4 5 |
$filename = 'temp-image.jpg'; $tempImage = tempnam(sys_get_temp_dir(), $filename); copy('https://my-cdn.com/files/image.jpg', $tempImage); return response()->download($tempImage, $filename); |
Note that, if you are getting the following errors in laravel apps, when you are working with laravel files or storage:
1: “class ‘app\http\controllers\file’ not found”.
Import File
in your controller file as follow:
1 |
use File; |
2: “class ‘app\http\controllers\response’ not found”.
Import Response
in your controller file as follow:
1 |
use Response; |
3: “class ‘app\http\controllers\storage’ not found”.
Import Storage
in your controller file as follow:
1 |
use Illuminate\Support\Facades\Storage; |