在 Codeigniter 中更新新图像时如何删除图像
How to delete an image when updated a new one in Codeigniter
我最近开始使用 Codeigniter 编码,对 php 来说还很新,
我制作了这个应用程序,用户可以在其中更新他的个人资料图片。但是当我返回上传时,我仍然在那里看到旧图像。
不知道如何执行此操作。
控制器:
public function profile_image_check() {
if($this->session->userdata('is_logged_in')){
$username = $this->session->userdata('v_member_username');
$url = $this->do_upload();
//$this->load->model('admin/my_profile_model');
//$title = $_POST['title'];
$this->my_profile_model->update_profile_image($url, $username);
}
}
private function do_upload() {
$type = explode('.', $_FILES['profile_image']['name']);
$type = $type[count($type)-1];
$url = "./uploads/profile/".uniqid(rand()).'.'.$type;
if(in_array($type, array('jpeg', 'gif', 'png', 'jpg')))
if(is_uploaded_file($_FILES['profile_image']['tmp_name']))
if(move_uploaded_file($_FILES['profile_image']['tmp_name'], $url));
return $url;
return '';
}
型号:
function update_profile_image($url, $username){
//$this->db->set('title', $title);
$this->db->set('profile_image', $url);
$this->db->where('v_member_username', $username);
$this->db->update('vbc_registered_members');
}
您需要使用 php unlink($filename)
功能在更新后删除文件。
这里有一些参考。 http://www.w3schools.com/php/func_filesystem_unlink.asp希望对您有所帮助。
我最近开始使用 Codeigniter 编码,对 php 来说还很新,
我制作了这个应用程序,用户可以在其中更新他的个人资料图片。但是当我返回上传时,我仍然在那里看到旧图像。
不知道如何执行此操作。
控制器:
public function profile_image_check() {
if($this->session->userdata('is_logged_in')){
$username = $this->session->userdata('v_member_username');
$url = $this->do_upload();
//$this->load->model('admin/my_profile_model');
//$title = $_POST['title'];
$this->my_profile_model->update_profile_image($url, $username);
}
}
private function do_upload() {
$type = explode('.', $_FILES['profile_image']['name']);
$type = $type[count($type)-1];
$url = "./uploads/profile/".uniqid(rand()).'.'.$type;
if(in_array($type, array('jpeg', 'gif', 'png', 'jpg')))
if(is_uploaded_file($_FILES['profile_image']['tmp_name']))
if(move_uploaded_file($_FILES['profile_image']['tmp_name'], $url));
return $url;
return '';
}
型号:
function update_profile_image($url, $username){
//$this->db->set('title', $title);
$this->db->set('profile_image', $url);
$this->db->where('v_member_username', $username);
$this->db->update('vbc_registered_members');
}
您需要使用 php unlink($filename)
功能在更新后删除文件。
这里有一些参考。 http://www.w3schools.com/php/func_filesystem_unlink.asp希望对您有所帮助。