In this tutorial you will learn about the Laravel 8 Link Storage Folder Example and its application with practical example.
In this Laravel 8 Link Storage Folder Example tutorial, I will show you how to link storage folder and access file from there in laravel 8. In this tutorial you will learn to link storage folder and access files from storage folder in laravel 8 application. In this Laravel 8 link storage folder example will cover the following topics:
- laravel link storage folder
- laravel storage:link not working
- laravel storage_path
- laravel link storage to public_html
- laravel link storage to public
- laravel storage link permission denied
- laravel storage link command
laravel link storage folder
laravel storage:link not working
laravel storage_path
laravel link storage to public_html
laravel link storage to public
laravel storage link permission denied
laravel storage link command
Laravel 8 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 |
Now, If you get laravel storage link permission denied error then you need to give permission for linking public storage directory in laravel 8. You may also missing a view directories in laravel_root/storage/
. In order to fix this, follow given commands:
1 2 3 4 5 |
cd {laravel_root}/storage mkdir -pv framework/views app framework/sessions framework/cache cd .. chmod 777 -R storage chown -R www-data:www-data storage |
If you cannot create symbolic links there is an option to have a special route One who reads and provides the image.
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 2 3 4 |
Route::get('storage/{filename}', function ($filename) { return Image::make(storage_path('public/' . $filename))->response(); }); |