[英]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. 我的代码出了问题,因为我的product_picture数据库字段充满了product_picture_1 ..,而其余部分(product_picture_2,product_picture_3,product_picture_4)没有输入数据库及其字段。
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. 处理完第一个文件后,您将重定向到另一页,因此将切断对表单其他元素的处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.