我无法访问公共磁盘上的文件内容

Jah*_*ion 1 permissions storage file-permissions laravel

在 Laravel 8 上

file_get_contents(asset('storage/list.json');
Run Code Online (Sandbox Code Playgroud)

给我吗:

ErrorException file_get_contents(http://localhost:8000/storage/list.json):无法打开流:HTTP 请求失败!

但是:http://localhost:8000/storage/list.json存在并且可以通过例如浏览器访问。在我的config/filesystem.php静脉注射中:

'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',
        ],
Run Code Online (Sandbox Code Playgroud)

我创建了符号链接:

php artisan storage:link
Run Code Online (Sandbox Code Playgroud)

命令。

我什至尝试按照 filesystem.php 中的描述手动将文件权限添加到存储文件夹,但没有任何变化。

Dee*_*apa 5

当您想要处理文件时,应该使用绝对路径。file_get_contents() 没问题。Laravel 中有一些可用的文件包装器,但在内部它们都使用 file_get_contents()。

尝试

file_get_contents(public_path(asset('storage/list.json')));
Run Code Online (Sandbox Code Playgroud)

如果从存储文件获取尝试

file_get_contents(storage_path('app/assets/list.json'));
Run Code Online (Sandbox Code Playgroud)

请参阅https://laravel.com/docs/5.5/helpers#method-resource-path