如何使用codeigniter更新上传的图像.必须来自数据库和目录



我是codeigniter的新手。我已经浏览了许多上传的帖子。我可以更新数据库中的图像链接,我选择的新图像成功上传,但我无法同时删除(取消链接(旧图像

这是我的控制器:

public function admin_profile_image()
{
if($this->session->userdata('login_answer') != null){
if('ADMIN'==$this->session->userdata('forredirect'))
{
$config['upload_path']          = './assets/uploads/profile_uploads/';
$config['allowed_types']        = 'jpeg|jpg|png';
$config['max_size']             = 100;
$config['max_width']            = 1024;
$config['max_height']           = 768;
$this->upload->initialize($config);                              

if($this->upload->do_upload('updateprofileimg'))
{
$image_data = $this->upload->data();                
$profile_img = base_url("assets/uploads/profile_uploads/".$image_data['file_name']);
$data = array(
'profile_photo'=>$profile_img                       
);
$this->load->model('update/updateAdmin');
if(!$this->updateAdmin->updateAdminImg($data))
{
$this->session->set_flashdata('profile_img_update_success','Profile photo updated successfully.');
redirect('update/admin_profile');
}
else
{
return FALSE;
}
}
else{
$this->session->set_flashdata('profile_img_update_unsuccess','Profile photo could not updated. Please try again.');
redirect('update/admin_profile');
}
}
}else{ 
return redirect('search'); 
}

}

这是型号:

public function updateAdminImg($data)
{
$this->db->set($data);
$this->db->where('login_id',$this->session->userdata('login_id'));
unlink($data);
$this->db->update('admin',$data);
}

以下是视图:

<?php echo form_open_multipart('update/admin_profile_image');?>
<div class="row">
<div class="col-lg-6">
<label>Browse and Choose New Profile Image (Image size, Image dimension): 
</label>
<div class="form-group" style="line-height:33px;">
<input type="file" name="updateprofileimg"  class="btn-primary btn-block">
</div>
</div>
<label class="col-lg-6 text-danger">
<?php if(isset($error)){ echo $error; } ?>
</label>
</div>
<?php echo form_submit(['type'=>'submit','class'=>'btn btn-primary','value'=>' Update']); ?>
<?php echo form_reset(['type'=>'reset','class'=>'btn btn-warning','value'=>' Reset ']); ?>
<?php echo form_close(); ?>

控制器

public function admin_profile_image()
{
$this->load->model('update/updateAdmin');
if($this->session->userdata('login_answer') != null){
if('ADMIN'==$this->session->userdata('forredirect'))
{
//First, Get old image from database
$old_image = $this->updateAdmin->get_old_image($this->session->userdata('login_id')); //Required parameter for image
$image_with_path = 'file-path/'.$old_image;
if(file_exists($image_with_path)){
unlink($image_with_path);
}    
$config['upload_path']          = FCPATH.'assets/uploads/profile_uploads'; //If your file uploading folder outside the application folder
//$config['upload_path']          = APPPATH.'assets/uploads/profile_uploads'; //If your file uploading folder inside the application folder
$config['allowed_types']        = 'jpeg|jpg|png';
$config['max_size']             = 100;
$config['max_width']            = 1024;
$config['max_height']           = 768;
$this->load->library('upload', $config);                              
if($this->upload->do_upload('updateprofileimg'))
{
$image_data = $this->upload->data();                
$profile_img = base_url("assets/uploads/profile_uploads/".$image_data['file_name']);
$data = array(
'profile_photo'=>$profile_img                       
);
if(!$this->updateAdmin->updateAdminImg($data))
{
$this->session->set_flashdata('profile_img_update_success','Profile photo updated successfully.');
redirect('update/admin_profile');
}
else
{
return true;
}
}
else{
$this->session->set_flashdata('profile_img_update_unsuccess',$this->upload->display_errors());
redirect('update/admin_profile');
}
}
}else{ 
return redirect('search'); 
}

}

型号

public function updateAdminImg($data)
{
$this->db->set($data);
$this->db->where('login_id',$this->session->userdata('login_id'));
$this->db->update('admin');
}
public function get_old_image($login_id){
return $this->db->get_where('admin', ['login_id' => $login_id])->row()->profile_photo;
}

相关内容

  • 没有找到相关文章

最新更新