Laravel 5.2 中的多张图片上传

Multiple Image Upload in Laravel 5.2

我终于可以上传和移动图片了,但现在我想在 Laravel 上创建多个上传图片。那可能吗?我必须使用数组来制作吗?

我可以稍微修改一下这段代码吗?

在我的 ProductController.php

$picture = '';

if ($request->hasFile('images')) {
    $file = $request->file('images');
    $filename = $file->getClientOriginalName();
    $extension = $file->getClientOriginalExtension();
    $picture = date('His').$filename;
    $destinationPath = base_path() . '\public\images/';
    $request->file('images')->move($destinationPath, $picture);
}

if (!empty($product['images'])) {
    $product['images'] = $picture;
} else {
    unset($product['images']);
}

谢谢。 注:我上面的代码来自Whosebug上的好心人,再次感谢;)

您从 $_POST 中输入的内容将作为一个数组传入。您需要做的就是遍历它:

$picture = '';

if ($request->hasFile('images')) {
    $files = $request->file('images');
    foreach($files as $file){
        $filename = $file->getClientOriginalName();
        $extension = $file->getClientOriginalExtension();
        $picture = date('His').$filename;
        $destinationPath = base_path() . '\public\images/';
        $request->file('images')->move($destinationPath, $picture);
    }
}

在您的前端表单中,您必须使用像

这样的字段属性名称
name="images[]"

你的控制器代码应该是这样的。

$picture = '';
if ($request->hasFile('images')) {
    $files = $request->file('images');
    foreach($files as $file){
        $filename = $file->getClientOriginalName();
        $extension = $file->getClientOriginalExtension();
        $picture = date('His').$filename;
        $destinationPath = base_path() . '\public\images';
        $file->move($destinationPath, $picture);
    }
}

if (!empty($product['images'])) {
    $product['images'] = $picture;
} else {
    unset($product['images']);
}

Slight modified code to upload multiple image.

 public function store(Request $request)
 {
         $pid = $request->input('pid');
         $input = $request->file('images');

         $picture = array();
         if($request->hasFile('images')) :
            foreach ($input as $item):
              $extension = $item->getClientOriginalName();
              $name = date('Ymd') . '.' . $extension;
              $destinationPath = base_path() . '/uploads/images/';
              $item->move($destinationPath, $name);
              $arr[] = $name;
            endforeach;
            $picture = implode(",", $arr);
         else:
            $picture = '';
         endif;

         DB::table('document')->insert(array('pid' => $pid,'image' => $picture)); 
         Session::flash('message', 'Multiple pictures are uploaded successfully');
         return redirect('/image-upload');
 }