我已经为三个上传文件编写了代码点火器。当我更新一个或两个时,第三个将覆盖我以前上传的数据库中的空白数据。如何使它只更新我需要的文件。我试过如果!空$_FILES,但我的头开始冒烟:(
<input type="file" class="form-control" name="userfile[]">Front
<input type="file" class="form-control" name="userfile[]">Back
<input type="file" class="form-control" name="userfile[]">Side
这是我的控制器
public function prosesUpdate2(){
$data = $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();
}
$files = array(
'front' => $dataInfo[0]['file_name'],
'back' => $dataInfo[1]['file_name'],
'side' => $dataInfo[2]['file_name']
// 'userdatecreate' => date('Y-m-d H:i:s')
);
$result_set = $this->update_building->db_update($files, $data);
$this->session->set_flashdata('file_success', 'Upload File Success!');
}
模型
public function db_update($data,$id)
{
$this->db->where('id', $id);
$this->db->update('allbuidingdata', $data);
}
请在更新之前检查已发布的文件。
稍微修改一下代码。
public function prosesUpdate2(){
$data = $this->input->post('id');
$this->load->library('upload');
$dataInfo = array();
$files = $_FILES;
$files_to_update = array();
$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();
}
$files_to_update= array(
'userdatecreate' => date('Y-m-d H:i:s')
);
if($dataInfo[0]['file_name']){
$files_to_update['front'] = $dataInfo[0]['file_name'];
}
if($dataInfo[1]['file_name']){
$files_to_update['back'] = $dataInfo[1]['file_name'];
}
if($dataInfo[2]['file_name']){
$files_to_update['side'] = $dataInfo[2]['file_name'];
}
$result_set = $this->update_building->db_update($files_to_update, $data);
$this->session->set_flashdata('file_success', 'Upload File Success!');
}
您忘记将字段名放在do_upload函数中
$this->upload->do_upload('userfile');
希望这能有所帮助!