我想删除数据库中的图像以及文件夹
控制器
function delete_projects(){
$this->load->model('m_insert_data');
$this->m_insert_data->delete_projects();
redirect('../c_home/projects', 'refresh');
}
模型
function delete_projects(){
$this->db->where('p_id',$this->uri->segment(3));
$this->db->delete('projects');
}
答案 0 :(得分:1)
您的delete
语法正确,因此您可能正在将其从数据库中删除。
看起来您需要做的就是将其从文件系统中删除。为此,您可以使用unlink
。
然而,我可能会以不同的方式构建它:
function delete_projects(){
$this->load->model('m_insert_data');
// You will want this ID more than once.
$id = $this->uri->segment(3);
$this->m_insert_data->remove_file($id);
$this->m_insert_data->delete_projects($id);
redirect('../c_home/projects', 'refresh');
}
模型
function remove_file($id){
$query = $this->db->get_where('projects', array('p_id' => $id))->result();
if($query) {
// assumes that there is a column "path" in the projects table
// which represents the path to the file
unlink($query->path);
}
}
function delete_projects($id){
// generally a good idea to have functions like this know as little
// about the outside world as possible. So pass $id as a param
$this->db->where('p_id',$id);
$this->db->delete('projects');
}