【问题标题】:i cannot access to file content on public disk我无法访问公共磁盘上的文件内容
【发布时间】:2021-01-18 01:58:25
【问题描述】:

在 Laravel 8 上

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

给我:

错误异常 file_get_contents(http://localhost:8000/storage/list.json): 失败 打开流:HTTP 请求失败!

但是:http://localhost:8000/storage/list.json 存在,例如可以通过浏览器访问。 在我的config/filesystem.phpiv'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',
        ],

我创建了符号链接:

php artisan storage:link

命令。

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

【问题讨论】:

    标签: laravel permissions storage file-permissions


    【解决方案1】:

    当你想处理文件时,你应该使用绝对路径。 file_get_contents() 很好。 laravel 中有一些文件包装器可用,但在内部它们都使用 file_get_contents()。

    试试

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

    如果从存储文件中获取尝试

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

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

    【讨论】:

    • 使它起作用的方法是在您建议的第一种情况下在“storage”之前添加“'app/public/”,好吧,他们喜欢绝对路径,但对我来说太奇怪了甚至可以通过 http 访问的东西不在本地!非常感谢'file_get_contents(storage_path('app/public/list.json'));'
    • 如果您可以访问http://localhost:8000/storage/list.json,则表示您的应用程序不安全。这意味着人们可以访问您的存储和公共目录。不要仅仅因为在生产或本地更容易而将存储的文件夹权限设置为777。
    • 如前所述,文件权限是在“config/filesystem.php”中设置的,但似乎 def 不起作用,也没有选择文件夹权限和结构!
    • 您需要使用 ssh 或从命令行执行此操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多