Laravel 9 Download File From URL to Public Storage Folder

Laravel 9 Download File From URL to Public Storage Folder

Laravel 9 download file from URL to storage. In this tutorial, we will learn how to download or display files from public storage folders in laravel apps.

Laravel 9 Download File From URL to Public Storage Folder

Follow the below steps and easily download files from public stroage folder. And as well as display files on laravel blade views:

Steps 1: Routes

First of all, we need to add the following routes on web.php file. So navigate to routes folder and open web.php file then update the following routes as follow:

  use App\Http\Controllers\FileController;


  Route::get('view', [FileController::class, 'index']);
  Route::get('get/{filename}', [FileController::class, 'getfile']);

Step 2: Create Controller File

Next, Navigate to app/controllers and create controller file named FileController.php. Then update the following methods as follow:

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.

If we want to display files on blade views, so we can update the following methods into controller file:

$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 code gets the image files from the public storage folder and extract the name of these files and we pass them to view.

Step 3: Create Blade View

Now, Navigate to resources\view folder. And create one blade view file named show.blade.php. Then update the following code into it:

 @foreach($images as $image)
 
  <div> 
     <img src="{{route('getfile', $image)}}"  class="img-responsive" />
  </div>
                  
 
  @endforeach

In Laravel 5,6,7,8,9 we can do this for a local file to download it:

return response()->download('path/to/file/image.jpg');

There’s no magic, we should download external image using copy() function, then send it to user in the response:

$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 we are getting the following errors in laravel apps, when we are working with laravel files or storage:

1: “class ‘app\http\controllers\file’ not found”.

Import File in controller file as follow:

use File;

2: “class ‘app\http\controllers\response’ not found”.

Import Response in controller file as follow:

use Response;

3: “class ‘app\http\controllers\storage’ not found”.

Import Storage in controller file as follow:

use Illuminate\Support\Facades\Storage;

Conclusion

In this tutorial, we have learned how to download files from public storage folder in laravel apps with example.

Recommended Laravel Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

One reply to Laravel 9 Download File From URL to Public Storage Folder

  1. Great tutorial on how to download files from a URL and save them to the public storage folder using Laravel 9 – thanks for sharing!

Leave a Reply

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