Search code examples
laravelpermissionsstoragefile-permissions

i cannot access to file content on public disk


On Laravel 8

file_get_contents(asset('storage/list.json');

Give me:

ErrorException file_get_contents(http://localhost:8000/storage/list.json): failed to open stream: HTTP request failed!

But: http://localhost:8000/storage/list.json exist and it is accesible via browser for example. On my config/filesystem.php iv'e:

'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',
        ],

And ive created the symbolic link with:

php artisan storage:link

command.

I've tried even to add manually file perssion to storage folder as described in the filesystem.php but nothing change.


Solution

  • You should use absolute paths when you want to work with files. file_get_contents() is fine. There are some file wrappers available in laravel but internally they all use file_get_contents().

    Try

    file_get_contents(public_path(asset('storage/list.json')));
    

    If getting from storage file try

    file_get_contents(storage_path('app/assets/list.json'));
    

    See https://laravel.com/docs/5.5/helpers#method-resource-path