Laravel Retrieve Images from storage to view

enter image description here

Remember put your folder in storage/app/public/

Create the symbolic linksymbolic link to access this folder

php artisan storage:link

if you want to access profile images of 2 folder then do like this in your blade file

<img src="{{ asset('storage/2/images/'.$user->profile_image) }}" />

File not publicly accessible like you said then read file like this

$userid = session()->get('user')->id;
$contents = Storage::get($userid.'/file.jpg'); 

Assuming your file is at path storage/app/{$userid}/file.jpg and default disk is local check config/filesystems.php

File publicly accessible

If you want to make your file publicly accessible then store file inside this storage/app/public folder. You can create subfolders inside it and upload there. Once you store file inside storage/app/public then you have to just create a symbolic link and laravel has artisan command for it.

php artisan storage:link

This create a symbolic link of storage/app/public to public/storage. Means now you can access your file like this

$contents = Storage::disk('public')->get('file.jpg'); 

here the file physical path is at storage/app/public/file.jpg and it access through symbolic link path public/storage/file.jpg

Suppose you have subfolder storage/app/public/uploads where you store your uploaded files then you can access it like this

$contents = Storage::disk('public')->get('uploads/file.jpg');

When you make your upload in public folder then you can access it in view

echo asset('storage/file.jpg'); //without subfolder uploads

echo asset('storage/uploads/file.jpg');

check for details https://laravel.com/docs/5.6/filesystem#configuration


Laravel 8

Controller

class MediaController extends Controller
{ 

    public function show(Request $request, $filename)
    {
        $folder_name = 'upload';

        $filename = 'example_img.jpeg';

        $path = $folder_name.'/'.$filename;

        if(!Storage::exists($path)){
            abort(404);
        }

        return Storage::response($path);
    }
}

Route

Route::get('media/{filename}', [\App\Http\Controllers\MediaController::class, 'show']);

Tags:

Laravel