How to Download File From Public Folder in Laravel 8


How to Download File From Public Folder in Laravel 8

In this tutorial, I am going to explain how you can download file from public folder in Laravel 8. So, in order to download any kind of image or file, you have to upload some files or images inside the public folder of the laravel application so that you can download the specific file from your blade template.

Also Read, How to Upload an Image To Public Folder in Laravel

Actually, there are two ways to download files from public folder in Laravel.

  • You can directly download the specific image by including the filename in the public path while downloading as shown in the controller below.
  • On the other hand, you can insert the filename in the database by creating a table, and later you can use that filename from the database at the time of downloading.

Required Steps to Download an Image From Public Folder in Laravel

  • Create the routes inside the web.php file of the routes folder as shown below
Route::get('/downloadfile','DownloadController@downloadfile');
  • Create a controller named ‘DownloadController’ using the artisan command as shown below
php artisan make:controller DownloadController

DownloadController.php:-

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use File;
use Response;
use DB;

class DownloadController extends Controller
{
    public function downloadfile()
    {
        $filepath = public_path('images/filename.JPG');
        return Response::download($filepath); 
    }
}

Note:- Do not forget to include the file and response on top of the controller as shown below;

use File;
use Repsonse;

Also Read, How to Display Image From Public Folder in Laravel 8

Conclusion:- I hope this tutorial will help you to understand. If you want to know more about file systems in Laravel then visit here.


Leave a Reply

Your email address will not be published. Required fields are marked *

Theme of 96 THEME.