【问题标题】:Change directory for uploading image更改上传图片的目录
【发布时间】:2021-07-15 12:35:12
【问题描述】:

如何更改上传图片的目录。 我想把它上传到 App/public/files 文件夹中。

我的代码在这里:

    public function update(Request $request, Organigramme $organigramme)
    {
    $id = $organigramme->id;
    $organigramme = Organigramme::find($id);
    $organigramme->matricule = $request->input('matricule');

    if ($request->has('profile_image'))
    {
        $image = $request->file('profile_image');
        $name = str_slug($request->input('matricule'));
        $folder = '/uploads/images/';
        $filePath = $folder . $name. '.' . $image->getClientOriginalExtension();
        $this->uploadOne($image, $folder, 'public', $name);

        $organigramme->profile_image = $filePath;
        $organigramme->save();
    }
    return view( 'admin.organigrammes.show', compact('organigramme'));
   }

public function uploadOne(UploadedFile $uploadedFile, $folder = null, $disk = 'public', $filename = null)
{
    $name = !is_null($filename) ? $filename : str_random(25);

    $file = $uploadedFile->storeAs($folder, $name.'.'.$uploadedFile->getClientOriginalExtension(), $disk);

    return $file;
}

【问题讨论】:

  • $folder = '/uploads/images/'; 改变这个?
  • 感谢卡姆莱什保罗

标签: laravel file-upload location storage


【解决方案1】:

在config/filesystems文件中添加新的保存路径。例如:

  'images' => [
        'driver' => 'local',
        'root' => base_path().'/app/public/files',
    ],

在代码本身,使用如下代码保存:

存储::disk('images')->put($path, $image);

【讨论】:

  • 谢谢。你拯救了我的一天
猜你喜欢
  • 2021-09-30
  • 2013-10-11
  • 2018-10-30
  • 2014-05-04
  • 2016-08-04
  • 1970-01-01
  • 2018-06-16
  • 1970-01-01
  • 2023-03-17
相关资源
最近更新 更多