In this tutorial you will learn about the Laravel 7/6 Link Storage Folder Example and its application with practical example.
In this Laravel 7/6 Link Storage Folder Example tutorial, I will show you how to link storage folder and access file from there in laravel. In this tutorial you will learn to link laravel storage folder and access files from storage folder.
Laravel 7/6 Link Storage Folder Example
Use the following command to link storage folder in laravel:
1 |
php artisan storage:link |
The above command creates a symbolic link from public/storage to storage/app/public for you. Now any file in /storage/app/public can be accessed via a link like:
1 |
http://yourdomain.com/storage/image.jpg |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Route::get('storage/{filename}', function ($filename) { $path = storage_path('public/' . $filename); if (!File::exists($path)) { abort(404); } $file = File::get($path); $type = File::mimeType($path); $response = Response::make($file, 200); $response->header("Content-Type", $type); return $response; }); |
1 2 3 4 5 6 7 8 9 10 11 12 |
Route::post('process', function (Request $request) { // cache the file $file = $request->file('photo'); // generate a new filename. getClientOriginalExtension() for the file extension $filename = 'profile-photo-' . time() . '.' . $file->getClientOriginalExtension(); // save to storage/app/photos as the new $filename $path = $file->storeAs('photos', $filename); dd($path); }); |
Now you can access your files the same way you do a symlink:
1 |
http://somedomain.com/storage/image.jpg |
1 2 3 4 |
Route::get('storage/{filename}', function ($filename) { return Image::make(storage_path('public/' . $filename))->response(); }); |