在 Codeigniter 中更新两个图像
Updating two image in Codeigniter
最近一次更新两张图片时发现了一些bug。我在第一张图片前使用 if (!empty($_FILES['ngo_logo']['name']))
,在第二张图片前使用 elseif (!empty($_FILES['ngo_license_pic']['name']))
。
这是我的完整控制器代码:
function editProfile(){
$ngo_id = $this->profile_model->editNgoProfile();
/************* File Upload ************/
if (!empty($_FILES['ngo_logo']['name'])) {
$config['upload_path'] = './assets/uploads/ngo_logo/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|doc|docx|zip|xls';
$config['overwrite'] = true;
$config['remove_spaces']=true;
$config['max_size']='10600';// in KB
$this->load->library('upload', $config);
$filetype = $_FILES['ngo_logo']['type'];
$file_name = $_FILES['ngo_logo']['name'];
if($filetype == "image/jpg")
$file_type='jpg';
else if ($filetype == "image/jpeg")
$file_type='jpeg';
else if ($filetype == "image/gif")
$file_type='gif';
else if($filetype == "image/jpeg")
$file_type='jpg';
else if($filetype == "image/pjpeg")
$file_type='pjpeg';
else if($filetype == "image/png")
$file_type='png';
$_FILES['ngo_logo']['name']=$ngo_id.'.'.$file_type;
$this->upload->do_upload('ngo_logo');
$up_dtat = array('ngo_logo' => $_FILES['ngo_logo']['name']);
$this->db->where('ngo_id',$ngo_id);
$this->db->update('tbl_ngo_users',$up_dtat);
}
elseif (!empty($_FILES['ngo_license_pic']['name'])) {
$config['upload_path'] = './assets/uploads/ngo_license_pic/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|doc|docx|zip|xls';
$config['overwrite'] = true;
$config['remove_spaces']=true;
$config['max_size']='10600';// in KB
$this->load->library('upload', $config);
$filetype = $_FILES['ngo_license_pic']['type'];
$file_name = $_FILES['ngo_license_pic']['name'];
if($filetype == "image/jpg")
$file_type='jpg';
else if ($filetype == "image/jpeg")
$file_type='jpeg';
else if ($filetype == "image/gif")
$file_type='gif';
else if($filetype == "image/jpeg")
$file_type='jpg';
else if($filetype == "image/pjpeg")
$file_type='pjpeg';
else if($filetype == "image/png")
$file_type='png';
$_FILES['ngo_license_pic']['name']=$ngo_id.'.'.$file_type;
$this->upload->do_upload('ngo_license_pic');
$up_dtat = array('ngo_license_pic' => $_FILES['ngo_license_pic']['name']);
$this->db->where('ngo_id',$ngo_id);
$this->db->update('tbl_ngo_users',$up_dtat);
}
redirect('NGO_Profile');
}
我的问题是只有第一张图片会更新,而另一张不会更新。
--> 如果我只上传一张图片然后它会更新
--> 如果我只上传第一张图片然后第一张图片更新
--> 以后如果我只上传第二张图片,那么第二张图片只会更新...
实际上我需要正确的解决方案,不能一次更新两个图像的主要原因是什么。
将elseif (!empty($_FILES['ngo_license_pic']['name'])) {
改为if(...)
elseifs 仅在 A. 第一个 if 条件为假 B. elseif 条件为真时触发。
最近一次更新两张图片时发现了一些bug。我在第一张图片前使用 if (!empty($_FILES['ngo_logo']['name']))
,在第二张图片前使用 elseif (!empty($_FILES['ngo_license_pic']['name']))
。
这是我的完整控制器代码:
function editProfile(){
$ngo_id = $this->profile_model->editNgoProfile();
/************* File Upload ************/
if (!empty($_FILES['ngo_logo']['name'])) {
$config['upload_path'] = './assets/uploads/ngo_logo/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|doc|docx|zip|xls';
$config['overwrite'] = true;
$config['remove_spaces']=true;
$config['max_size']='10600';// in KB
$this->load->library('upload', $config);
$filetype = $_FILES['ngo_logo']['type'];
$file_name = $_FILES['ngo_logo']['name'];
if($filetype == "image/jpg")
$file_type='jpg';
else if ($filetype == "image/jpeg")
$file_type='jpeg';
else if ($filetype == "image/gif")
$file_type='gif';
else if($filetype == "image/jpeg")
$file_type='jpg';
else if($filetype == "image/pjpeg")
$file_type='pjpeg';
else if($filetype == "image/png")
$file_type='png';
$_FILES['ngo_logo']['name']=$ngo_id.'.'.$file_type;
$this->upload->do_upload('ngo_logo');
$up_dtat = array('ngo_logo' => $_FILES['ngo_logo']['name']);
$this->db->where('ngo_id',$ngo_id);
$this->db->update('tbl_ngo_users',$up_dtat);
}
elseif (!empty($_FILES['ngo_license_pic']['name'])) {
$config['upload_path'] = './assets/uploads/ngo_license_pic/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|doc|docx|zip|xls';
$config['overwrite'] = true;
$config['remove_spaces']=true;
$config['max_size']='10600';// in KB
$this->load->library('upload', $config);
$filetype = $_FILES['ngo_license_pic']['type'];
$file_name = $_FILES['ngo_license_pic']['name'];
if($filetype == "image/jpg")
$file_type='jpg';
else if ($filetype == "image/jpeg")
$file_type='jpeg';
else if ($filetype == "image/gif")
$file_type='gif';
else if($filetype == "image/jpeg")
$file_type='jpg';
else if($filetype == "image/pjpeg")
$file_type='pjpeg';
else if($filetype == "image/png")
$file_type='png';
$_FILES['ngo_license_pic']['name']=$ngo_id.'.'.$file_type;
$this->upload->do_upload('ngo_license_pic');
$up_dtat = array('ngo_license_pic' => $_FILES['ngo_license_pic']['name']);
$this->db->where('ngo_id',$ngo_id);
$this->db->update('tbl_ngo_users',$up_dtat);
}
redirect('NGO_Profile');
}
我的问题是只有第一张图片会更新,而另一张不会更新。
--> 如果我只上传一张图片然后它会更新
--> 如果我只上传第一张图片然后第一张图片更新
--> 以后如果我只上传第二张图片,那么第二张图片只会更新...
实际上我需要正确的解决方案,不能一次更新两个图像的主要原因是什么。
将elseif (!empty($_FILES['ngo_license_pic']['name'])) {
改为if(...)
elseifs 仅在 A. 第一个 if 条件为假 B. elseif 条件为真时触发。