Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kesavan_Kani's avatar

Multiple Image Upload

Hi, How to upload multiple image same like cpanel. Already image check and auto upload in cpanel. Same condition like multiple image upload.

0 likes
6 replies
Tray2's avatar

You create a form that has multipart, then you create a file input with a name iike file[], and then use the multiple. attribute.

Then you check if there is a file on the php side, and if it does, you loop over them to store them.

Tray2's avatar

@Kesavan_Kani That depends, are we talking the same filename, or are we talking the exact same file?

If we are talking the same filename, just do an ajax call, and query the images table, an if it exists, then inform the user.

If we are talking about the exact same file, the you need to do a comparison with a hash, at least md5, preferably sha1.

ranazaid123's avatar

public function store(Request $request) {

    // print_r($request->all());
    // die();
    //  dd($request->all());
    // dd(auth()->user()->usertype);

    $validator = Validator::make($request->all(), [
        'product_name' => 'required | max:191',
        'title_name' => 'required | max:191',
        'price' => 'required | max:191',
        'description' => 'required',
        'image_upload' => 'required',
        'admin_role' => ' | max:191',

    ]);
    if ($validator->fails()) {
        return response()->json([
            'status' => 400,
            'errors' => $validator->messages(),
        ]);
    } else {
        $Admin = new AdminProduct;

        $Admin->product_name = $request->input('product_name');
        $Admin->title_name = $request->input('title_name');
        $Admin->price = $request->input('price');
        $Admin->description = $request->input('description');
        $Admin->send_status = $request->input('admin_role');
        $Admin->save();
        $Admin->id;

        if ($request->image_upload) {

            foreach ($request->image_upload as $key => $image) {

                $imageName = time() . rand(1, 99) . '.' . $image->extension();

                $image->move(public_path('images'), $imageName);

                $gallery = new Image;
                $gallery->images = $imageName;
                $gallery->product_id = $Admin->id;
                $gallery->save();
            }
        }

        return response()->json([
            'status' => 200,
        ]);
    }
}

form @csrf

                            <div class="form-group mb-3">
                                <label for="">Product Name </label>
                                <input type="text" name="product_name" class="product_name form-control">
                            </div>
                            <div class="form-group mb-3">
                                <label for="">Title Name </label>
                                <input type="text" name="title_name" class="title_name form-control">
                            </div>
                            <div class="form-group mb-3">
                                <label for=""> Price </label>
                                <input type="text" name="price" class="price form-control">
                            </div>
    
                            <input type="hidden" name="admin_role" value="admin" class="admin_role form-control">
    
    
                            <div class="form-group mb-3">
                                <label for="">Description </label>
                                <textarea name="description" id="description_get" class="description form-control"></textarea>
    
                            </div>
                            <div class="form-group mb-3">
                                <label for="">Images </label>
                                <input type="file" id="image-upload" name="image_upload[]" class="images form-control"
                                    multiple>
                            </div>
    
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary add_product_admin">Save </button>
                    </div>
                    </form>
    

    jquery:

    // {{-- Insert Data --}} $("#add_employee_form").submit(function(e) { e.preventDefault(); const fd = new FormData(this); $.ajax({ url: "{{ route('add_product.store') }}", method: 'post', data: fd, cache: false, contentType: false, processData: false, dataType: 'json', success: function(response) { $("#add_employee_form").trigger("reset"); $('#sub_category_name').empty(); // $('#sub_category_name').refresh(); fetch_data(); fetchstudent(); if (response.status == 200) { Swal.fire( 'Added!', 'Employee Added Successfully!', 'success' );

                            //   $('#AddStudentModal').find('#description').val("");
                            $('#saveform_errList').hide();
                            // $('#success_message').text(response.message);
                            $('#AddStudentModal').modal('hide');
                            //  $('#AddStudentModal').find('input').val("");
                            fetchstudent();
                        } else {
                            $('#saveform_errList').html();
                            $('#saveform_errList').addClass(
                                'alert alert-danger');
                            $.each(response.errors, function(key, err_values) {
                                $('#saveform_errList').append('<li>' +
                                    err_values +
                                    '</li>');
                            });
    
                        }
    
                    }
                });
            });
    
    
            // {{-- Insert Data end  --}}
    

    Please or to participate in this conversation.