使用Laravel 5.8将图像上传到两个不同的文件夹位置 [英] Upload image to two different folder locations using Laravel 5.8

查看:96
本文介绍了使用Laravel 5.8将图像上传到两个不同的文件夹位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码仅将图像保存在一个文件夹中.我想同时在两个不同的文件夹中上传图片, 例子 文件夹一 和 文件夹二

This Code save image in only one folder. I want to upload the image at the same time in two different folders, example folder-one and folder-two

我的控制者

protected function validator(array $data)
{
    return Validator::make($data, [

       'photo_jpeg' => 'required|image|mimes:jpeg,png,jpg|max:2048',


    ]);
}
protected function create(array $data)
{
    $photo_jpeg= time() . '.' . $data['photo_jpeg']->getClientOriginalExtension();
            $data['photo_jpeg']->move(base_path() . 'public/folder-one', $photo_jpeg);

return user::create([

        'photo_jpeg' => $photo_jpeg,


       ]);

}

推荐答案

请确保这些内容.如果要在其他位置更新文件.

Please make sure these things. if you are going to update file on different location.

  1. 该文件夹必须具有可写权限.
  2. 目录路径应定义为绝对路径,并指向正确的位置.

现在更改,请按照以下步骤验证代码更改.

Now change verify the changes in code as follow.

$fileName = time() . '.' .$request->file('User_jpeg')->getClientOriginalExtension();

$storageLocation = '../../WEBSITE-FILE/TEAM/USER';  //it should be absolute path of storage location.

$request->file('User_jpeg')
        ->storeAs($storageLocation, $fileName);

$request->file('User_jpeg')
        ->storeAs($storageLocation . '/User_Profile_Image', $fileName);

根据请求的当前状态,尝试此操作.

As per the requested current status, try this.

public function store(Request $request) { 
    $this->validate($request, [ 'image' => 'required|image|mimes:jpeg,png,jpg|max:2048', ]); $input['image'] = time().'.'.$request->image->getClientOriginalExtension();

    $request->image->move(public_path('folder-a'), $input['image']);

    $fileSrc = public_path('folder-a') . $input['image'];
    $fileDest = public_path('folder-b') . $input['image'];

    \File::copy($fileSrc, $fileDest);

    Service::create($input); 

    return back()->with('success',' CREATED SUCCESSFULLY .'); 
}

这篇关于使用Laravel 5.8将图像上传到两个不同的文件夹位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆