CodeIgniter:如何将多个图像上传到数据库中

时间:2015-05-13 16:37:54

标签: php codeigniter upload

所以我使用可以进行多次上传的表单。

This is my database field :
ID
product_picture_1
product_picture_2
product_picture_3
product_picture_4

这是我的观点:

<?=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>

这是我的控制者:

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

                        }
                    }
            }

这是我的模特:

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

我的代码出错了,因为我的product_picture数据库字段中充满了product_picture_1 .. 而其余的(product_picture_2,product_picture_3,product_picture_4)未输入数据库及其字段。

有可能吗?

感谢

1 个答案:

答案 0 :(得分:0)

你的问题在于这一行:

redirect('product');

处理完第一个文件后,您将重定向到另一个页面,因此表格中其他元素的处理将被切断。