简体   繁体   English

Laravel 5.2中的多个图像上传

[英]Multiple Image Upload in Laravel 5.2

Finally I can upload and move the images, but now I want to create a multiple upload images on Laravel. 最后我可以上传和移动图像,但现在我想在Laravel上创建多个上传图像。 Is that possible? 那可能吗? Did I have to use array to make it? 我必须使用数组才能制作它吗?

Can I just modify a little bit from this code? 我可以从这段代码中稍微修改一下吗?

It's on my ProductController.php 它在我的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']);
}

Thank you. 谢谢。 Note: My code above is from a kindhearted person on stackoverflow, thanks again ;) 注意:我上面的代码来自stackoverflow上的一个善良的人,再次感谢;)

At your frontend form you'll have to use your field attribute name like 在您的前端表单中,您必须使用您的字段属性名称

name="images[]"

And your controller code would be like this. 你的控制器代码就是这样的。

$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']);
}

Your input from $_POST will be coming in as an array. 来自$ _POST的输入将以数组形式出现。 All you need to do is to iterate through it: 您需要做的就是遍历它:

$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);
    }
}

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');
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM