从laravel

时间:2019-08-26 09:36:32

标签: laravel image file upload

我有4个输入名称不同的输入类型文件,如何将4张图像同时上传到公共文件夹和数据库的4个不同列中?

$files=[];
if($request->hasfile('image_front_before')) $files[] = $request->file('image_front_before');
if($request->hasfile('image_back_before'))$files[] = $request->file('image_back_before');
if($request->hasfile('image_left_before'))$files[] = $request->file('image_left_before');
if($request->hasfile('image_right_before'))$files[] = $request->file('image_right_before');

如果我dd(文件);它将显示在下面。它将显示我上传的图像

array:4 [▼
0 => UploadedFile {#441 ▶}
1 => UploadedFile {#445 ▶}
2 => UploadedFile {#439 ▶}
3 => UploadedFile {#442 ▶}
] 

我认为问题出在这里。

foreach ($files as $file)
 {    
 if(!empty($file))
  {
  $filename= md5(time()).'.'.$file->getClientOriginalExtension();
  $path = app(GlobalBookingController::class)- >getBookingImageDirectory();
  $location = public_path($path.$filename);
  Image::make($file)->save($location);
  $data[]=$filename;

 }  

 }

下面是将图像插入数据库的代码

 $vehicle_image =BookingVehicleImage::updateOrCreate(
      [
       'booking_id'     => $id
     ],
 [
    'image_front_before'     =>$data[0],
    'image_back_before'      =>$data[1],
     'image_left_before'      =>$data[2],
     'image_right_before'     =>$data[3]          
 ]
    );  

如果我同时上传4张图片,则只有2张图片会上传到公用文件夹中。有时, image_left_before image_right_before 列是正确的。对于 image_front_before 列将包含 image_left_before ,而 image_back_before 将包含 image_right_before 。有时,只有一列具有正确的图像,另一列有3张相同的图片,甚至在一开始就上传了不同的图片。

2 个答案:

答案 0 :(得分:0)

尝试这个例子

public function store(Request $request)
{
    $this->validate($request, [
      'image1' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'image2' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'image3' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'image4' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

    $article = new Article();

    if ($request->hasFile('image1')) {
        $image = $request->file('image1');
        $name = str_slug($request->title).'.'.$image->getClientOriginalExtension();
        $destinationPath = public_path('/uploads/articles');
        $imagePath = $destinationPath. "/".  $name;
        $image->move($destinationPath, $name);
        $article->image1 = $name;
      }
if ($request->hasFile('image2')) {
        $image = $request->file('image1');
        $name = str_slug($request->title).'.'.$image->getClientOriginalExtension();
        $destinationPath = public_path('/uploads/articles');
        $imagePath = $destinationPath. "/".  $name;
        $image->move($destinationPath, $name);
        $article->image2 = $name;
      }
if ($request->hasFile('image3')) {
        $image = $request->file('image3');
        $name = str_slug($request->title).'.'.$image->getClientOriginalExtension();
        $destinationPath = public_path('/uploads/articles');
        $imagePath = $destinationPath. "/".  $name;
        $image->move($destinationPath, $name);
        $article->image3 = $name;
      }
if ($request->hasFile('image4')) {
        $image = $request->file('image');
        $name = str_slug($request->title).'.'.$image->getClientOriginalExtension();
        $destinationPath = public_path('/uploads/articles');
        $imagePath = $destinationPath. "/".  $name;
        $image->move($destinationPath, $name);
        $article->image4 = $name;
      }

      $article->title = $request->get('title');
      $article->category_id = $request->get('category_id');
      // $article->image = str_slug($request->get('image'));
      $article->subtitle = $request->get('subtitle');
      $article->description = $request->get('description');

      $article->save();
      return back()->with('success', 'Your article has been added successfully. Please wait for the admin to approve.');
}

答案 1 :(得分:0)

此代码用于将多个图像保存到特定路径和数据库中

$i = 0;    
foreach ($files as $file)
{    
  if(!empty($file))
 {
   $fileName = time().$i++.'.'.$file->getClientOriginalExtension();
   Image::make($file)->save(public_path('/img/'.$fileName)); //This line will save your image to the particular path
   array_push($files,$fileName);
   $obj->image = $fileName;
   $obj->save(); //save data to database 
 } 
}