Laravel 5.5 - 上传到公共文件夹

时间:2017-11-20 13:03:09

标签: laravel file storage public

我试图将文件存储在公用文件夹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'),
    ],

],

1 个答案:

答案 0 :(得分:1)

当您致电Storage::put时,Laravel将使用默认磁盘“本地”。

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

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

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

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

相关问题