File not found at path in laravel 7x

sajadsholi picture sajadsholi · Mar 24, 2020 · Viewed 7.1k times · Source

I have an image at storage/public and I want access that with Storage library but i get error "File not found at path"

I search a lot in the internet and I don't get any solution for my problem so please help me.

my code :

$filename = '123.jpg';
$path = storage_path()."/".$filename;
Storage::disk('local')->get($path);
dd($contents);

the error :

Illuminate\Contracts\Filesystem\FileNotFoundException
C:\xampp\htdocs\admin\storage/123.jpg

the things i tried :

php artisan storage:link

the filesystem.php

<?php

return [
    'default' => env('FILESYSTEM_DRIVER', 'local'),

    'cloud' => env('FILESYSTEM_CLOUD', 's3'),

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
            'permissions' => [
                'file' => [
                    'public' => 0664,
                    'private' => 0600,
                ],
                'dir' => [
                    'public' => 0775,
                    'private' => 0700,
                ],
            ]
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL') . '/storage',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
        ],

    ],

    'links' => [
        public_path('storage') => storage_path('app/public'),
    ],

];

Answer

Sehdev picture Sehdev · Mar 24, 2020

Your file is under storage/public folder but you trying to access it from storage folder.

Check your error path C:\xampp\htdocs\admin\storage/123.jpg

Change

$path = storage_path()."/".$filename;

to

$path = storage_path('public/' . $filename);