How to Link Storage Folder to the Public in Laravel 9?
Hi Developer,
I am going to show you an example of How to Link a Storage Folder to a Public in Laravel 9?. We will use Download Files From Public Storage Folder in Laravel 9. This article will give you a simple example of Laravel 9 Link Storage Folder to Public Example Tutorial. you will learn Laravel 9 Link Storage Folder Example. you will do the following things how to link up storage files Laravel 9.
Many times we have issues with How to fix the storage link issue of the Laravel project on production by using the shared hosting plan so we have added the perfect solution here for you.
What types of issues come with link images with storage folders we have solved them here:
- 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
Download Laravel
Let us begin the tutorial by installing a new Laravel application. if you have already created the project, then skip the following step.
composer create-project laravel/laravel example-app
Let’s link your files with the storage folder in Laravel using the following command:
php artisan storage:link
It turns out I was missing a view directory in laravel_root/storage/. To fix this use the following steps:
1. cd {laravel_root}/storage
2. mkdir -pv framework/views app framework/sessions framework/cache
3. cd ..
4. chmod 777 -R storage
5. chown -R www-data:www-data storage
That creates a symlink from public/storage to storage/app/public for us and that’s all there is to it. Now any file in /storage/app/public can be accessed via a link like:
http://yourdomain.com/storage/image.jpg
Create Route
In this first step, we will engender two routes. so open your routes/web.php file and add the following route.
routes/web.php
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;
});
Next to
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 we can access files the same way we do a symlink:
http://somedomain.com/storage/image.jpg
If we are using the Intervention Image Library, we can make things more successful by using its built-in response method:
Route::get('storage/{filename}', function ($filename)
{
return Image::make(storage_path('public/' . $filename))->response();
});
It will help you...
2 Comments
(k)
ReplyDelete:-b
ReplyDeleteCAN FEEDBACK
Emoji