简体   繁体   中英

CodeIgniter : How to upload multiple image into database

So I working with form that can do multiple uploads.

This is my database field :
ID
product_picture_1
product_picture_2
product_picture_3
product_picture_4

This is my view :

<?=form_open_multipart('product/insert_product');?>
   <input type="file" multiple class="default" name="product_picture_1" required/>
   <input type="file" multiple class="default" name="product_picture_2" required/>
   <input type="file" multiple class="default" name="product_picture_3" required/>
   <input type="file" multiple class="default" name="product_picture_4" required/>

<button class="btn btn-success" type="submit">Submit</button>
</form>

this is my controller :

            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '2048000';
            $config['max_width']  = '25000';
            $config['max_height']  = '20000';

            $this->load->library('upload', $config);
            $this->upload->initialize($config);


            if ($this->form_validation->run() === FALSE)
            {
                    $data['product_data'] = $this->product_model->list_product();
                    $data['group_data'] = $this->group_model->list_group();
                    $this->load->view('admin/product_view', $data);
            }
            else
            {
                    foreach($_FILES as $field_name => $file)
                    {
                        if ( ! $this->upload->do_upload($field_name))   
                        {
                                $error = array('error' => $this->upload->display_errors());

                                $this->load->view('admin/product_view', $error);
                        }
                        else
                        {

                                $upload_image = $this->upload->data();

                                $data = array (
                                                'group_id' => $this->input->post('group_id'),
                                                'product_name' => $this->input->post('product_name'),
                                                'product_description' => $this->input->post('product_description'),
                                                'product_picture_1' => $upload_image['file_name'],
                                                'product_picture_2' => $upload_image['file_name'],
                                                'product_picture_3' => $upload_image['file_name'],
                                                'product_picture_4' => $upload_image['file_name']
                                                );

                                $this->product_model->add_product($data);
                                redirect('product');

                        }
                    }
            }

and this is my model :

    function add_product($data)
    {
            //untuk insert data ke table product
            $this->db->insert('product', $data);
    }

My code going wrong, because my product_picture database field full of product_picture_1.. while the rest (product_picture_2, product_picture_3, product_picture_4) are not entered into the database and their fields.

Is it possible ?

thanks

Your problem is with this line:

redirect('product');

After the first file is processed, you're redirecting to another page, so the processing of the other elements of the form is cut off.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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