我在代码点火器中执行多个图像上传相同的代码用于更新多个图像但在更新时间我更改名称我不更改图像然后单击确定按钮名称更新但图像是缺少但我更改了图片只更新..为什么?
我的控制器
function update_products()
{
$id = $this->input->post('id');
$this->load->library('upload');
$dataInfo = array();
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$dataInfo[] = $this->upload->data();
}
$data = array(
'name' => $this->input->post('name'),
'prod_image' => $dataInfo[0]['file_name'],
'prod_image1' => $dataInfo[1]['file_name'],
'prod_image2' => $dataInfo[2]['file_name'],
'upadted_time' => date('Y-m-d H:i:s')
);
$stock = array(
'discount' => $this->input->post('discount'),
);
$this->db->where('prod_id', $id);
$this->db->update('tbl_products', $data);
$this->db->where('prods_id', $id);
$this->db->update('otc_product_discount', $stock);
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are Successfully updated!</div>');
redirect('index.php/admin/display_product_list');
}
}
&#13;
我的观点
<label for="type">Product Image</label>
<img src="<?php echo base_url("./resources/images/products/"); if($row->prod_image) echo $row->prod_image; else echo "no-img.jpg"; ?>" alt="" height="50" width="50"/>
<input type="file" id="med_img" class="file_upload" name="userfile[]" value="<?php echo $row->prod_image; ?>" />
<label for="type">Product Image2</label>
<img src="<?php echo base_url("./resources/images/products/"); if($row->prod_image1) echo $row->prod_image1; else echo "no-img.jpg"; ?>" alt="" height="50" width="50"/>
<input type="file" id="med_img" class="file_upload" name="userfile[]" value="<?php echo $row->prod_image1; ?>" />
<label for="type">Product Image3</label>
<img src="<?php echo base_url("./resources/images/products/"); if($row->prod_image2) echo $row->prod_image2; else echo "no-img.jpg"; ?>" alt="" height="50" width="50"/>
<input type="file" id="med_img" class="file_upload" name="userfile[]" value="<?php echo $row->prod_image2; ?>" />
&#13;
uplaod功能
private function set_upload_options()
{
//upload an image options
$config = array();
$config['upload_path'] = './resources/images/products/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
}
&#13;
答案 0 :(得分:1)
我认为需要在控制器中单独上传。
function update_products()
{
$id = $this->input->post('id');
$this->load->library('upload');
$this->upload->initialize($this->set_upload_options());
$data = array();
if($this->upload->do_upload('userfile')) {
$dataInfo = $this->upload->data();
$data['prod_image'] = $dataInfo['file_name'];
}
if($this->upload->do_upload('userfile1')) {
$dataInfo = $this->upload->data();
$data['prod_image1'] = $dataInfo['file_name'];
}
if($this->upload->do_upload('userfile2')) {
$dataInfo = $this->upload->data();
$data['prod_image2'] = $dataInfo['file_name'];
}
$data['name'] = $this->input->post('name'),
$data['upadted_time'] = date('Y-m-d H:i:s')
$stock = array(
'discount' => $this->input->post('discount'),
);
$this->db->where('prod_id', $id);
$this->db->update('tbl_products', $data);
$this->db->where('prods_id', $id);
$this->db->update('otc_product_discount', $stock);
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are Successfully updated!</div>');
redirect('index.php/admin/display_product_list');
}
更改FIlE
输入字段名称userfile
userfile1
和userfile2
如下:
<input type="file" id="med_img" class="file_upload" name="userfile"/>
<input type="file" id="med_img" class="file_upload" name="userfile1"/>
<input type="file" id="med_img" class="file_upload" name="userfile2"/>