Kesavan_Kani's avatar

Multiple Image Upload?

Hi I have upload multiple image in laravel. But only one image upload on server. How to upload multiple image?

if($request->hasfile('files')) {
            $image_upload = $request->file('files');
            // dd($image_upload);
            foreach($request->file('files') as $file)
            {
                $mextention = $file->getClientOriginalName();
                if (file_exists( public_path() . '/uploads/product_images/' . $mextention)) {
                  Flash::error('This Image Already Uploaded');
                  return redirect()->back();
                }
                else
                {
                  $image_example[] = $mextention;
                  $file->move('uploads/product_images', $mextention); 
                  MultipleImage::insert( [
                    'images'=>  $mextention,
                    'image_type' => 'product_image',
                    'active' => 1,
                  ]); 
                  // Flash::success('Product Image uploaded successfully');
                  // return redirect()->back();
                } 
                dd($image_example);
            }
        }
0 likes
12 replies
Vijay's avatar

Have you added attribute enctype="multipart/form-data" in form tag

and input tag as:

<input type="file"  name="files[]" multiple />
Sinnbeck's avatar

You are either returning or doing dd() inside the foreach, meaning it stops at the first one.

Try

$image_upload = $request->file('files');
dd($image_upload);
Kesavan_Kani's avatar

@Sinnbeck i have upload 3 image..But only one save the server

if($request->hasfile('files')) {
            $image_upload = $request->file('files');
            foreach($request->file('files') as $file)
            {
                $mextention = $file->getClientOriginalName();
                if (file_exists( public_path() . '/uploads/product_images/' . $mextention)) {
                  Flash::error('This Image Already Uploaded');
                  return redirect()->back();
                }
                else
                {
                  $image_example[] = $mextention;
                  $file->move('uploads/product_images', $mextention); 
                  MultipleImage::insert( [
                    'images'=>  $mextention,
                    'image_type' => 'product_image',
                    'active' => 1,
                  ]); 
                  Flash::success('Product Image uploaded successfully');
                  return redirect()->back();
                } 
                // dd($image_example);
            }
        }
Tray2's avatar

@Kesavan_Kani Of course not, you can't have the return inside the loop, it will return on first itteration.

You need to place it out side the loop.

if($request->hasfile('files')) {
            $image_upload = $request->file('files');
            foreach($request->file('files') as $file)
            {
                $mextention = $file->getClientOriginalName();
                if (file_exists( public_path() . '/uploads/product_images/' . $mextention)) {
                  Flash::error('This Image Already Uploaded');
                  return redirect()->back();
                }
                else
                {
                  $image_example[] = $mextention;
                  $file->move('uploads/product_images', $mextention); 
                  MultipleImage::insert( [
                    'images'=>  $mextention,
                    'image_type' => 'product_image',
                    'active' => 1,
                  ]); 
                } 
                // dd($image_example);
            }
                  Flash::success('Product Image uploaded successfully');
                  return redirect()->back();  

        }
Sinnbeck's avatar

@Kesavan_Kani And are you absolutely sure you dont hit the "This Image Already Uploaded" ? If you do, it wont handle any other images.

Or show your current code after changing it.

Please or to participate in this conversation.