使用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.
- 该文件夹必须具有可写权限.
- 目录路径应定义为绝对路径,并指向正确的位置.
现在更改,请按照以下步骤验证代码更改.
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 .');
}