Multiple Image Upload in Laravel 5.2
Asked Answered
F

3

7

Finally I can upload and move the images, but now I want to create a multiple upload images on 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

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

Fante answered 25/4, 2016 at 5:4 Comment(0)
K
14

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']);
}
Khrushchev answered 25/4, 2016 at 7:25 Comment(2)
what if I need to upload different images in different fields in database? means field name is different where <input> name is also different let's say!Palatine
it can be improved with: $destinationPath = public_path('images');Streeto
R
3

Your input from $_POST will be coming in as an array. 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);
    }
}
Railroader answered 25/4, 2016 at 6:59 Comment(2)
And about the form, did I have to use name="images[]" or just like name="images1", name="images2", name="images3"?Fante
Seems like the images temporary name has been recorded on my database, but it's fail to be moved.Fante
D
0

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');
 }
Dominions answered 22/8, 2019 at 16:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.