【问题标题】:Laravel 5.5 - Upload to public folderLaravel 5.5 - 上传到公共文件夹
【发布时间】:2018-05-03 17:08:00
【问题描述】:

我正在尝试将文件存储在公共文件夹 storage/app/public/ 中,但由于某种原因,Laravel 似乎只是将它放在私有文件夹 storage/app/ 中。

如果我理解正确,我应该将可见性设置为“公开”,但这似乎并没有改变任何东西:

Storage::put($fileName, file_get_contents($file), 'public');

当我调用 getVisibility 时,我会公开,所以这似乎工作正常:

Storage::getVisibility($fileName); // public

这些是我的 filesystems.php 中的设置:

'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

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

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_KEY'),
        'secret' => env('AWS_SECRET'),
        'region' => env('AWS_REGION'),
        'bucket' => env('AWS_BUCKET'),
    ],

],

【问题讨论】:

    标签: laravel file storage public


    【解决方案1】:

    当你调用Storage::put 时,Laravel 将使用默认的“本地”磁盘。

    本地磁盘将文件存储在其根目录:storage_path('app')。可见性与文件的存储位置无关。

    您需要选择public 磁盘将文件存储在其根目录:storage_path('app/public'),

    为此,您需要告诉 Laravel 在上传文件时使用哪个磁盘。基本上将您的代码更改为:

    Storage::disk('public')->put($fileName, file_get_contents($file), 'public');

    【讨论】:

    • 谢谢,这行得通!但我仍然不明白 put 方法中的“公共”参数是做什么的。我可以省略它(甚至将其设置为“私人”),一切仍然正常。我很困惑:P
    • 当您使用 amazon s3 作为磁盘时,可见性参数很有用。它将设置文件的可见性(是否可以公开访问。在您的情况下,使用本地磁盘时,它没有效果。
    猜你喜欢
    • 2019-01-27
    • 2020-05-20
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    • 2016-03-31
    • 2015-07-01
    相关资源
    最近更新 更多