无法从目录中删除图像,但数据成功从codeigniter中的数据库中删除

时间:2018-07-30 16:35:48

标签: php codeigniter codeigniter-3 delete-file

我无法从目录中删除图像,但是数据已成功从数据库中删除。请我帮忙。

下面是我的控制器

public function hapus(){

   $id  = $this->input->get('id');

   /* query showing image for deleting image first before delete it from database */
   $path = './asset/uploads/';
   $path1 = './asset/hasil_resize/';
   $arraydelete  = array('id'=>$id);
   $rowdel = $this->Model_upldgbr->get_byimage($arraydelete);

   /* the image delete from folder */
   @unlink($path.$path1.$rowdel->namafile);


   $this->Model_upldgbr->get_delete($arraydelete); 
   $this->session->set_flashdata("pesan", "<div class=\"col-md-12\"><div class=\"alert alert-danger\" id=\"alert\">Success deleting the image and data !!</div></div>");
   redirect('root/upload');


 }

这是我的模特

function get_delete($where){
       $this->db->where($where);
       $this->db->delete($this->tabel);
       return TRUE;
    }
//function for showing data one by one from the table
    function get_byimage($where) {
        $this->db->from($this->tabel);
        $this->db->where($where);
        $query = $this->db->get();
        if ($query->num_rows() == 1) {
            return $query->row();
        }
    }
}

删除按钮链接是这样的:

 <a href="<?=base_url()?>index.php/root/upload/hapus/?id=<?=$row->id?>"><i class="fa fa-trash-o"></i></a>

1 个答案:

答案 0 :(得分:1)

希望这对您有帮助:

使用两个unlink方法通过使用FCPATH从两个文件夹中删除文件,应该是这样

$path = FCPATH.'asset/uploads/';
$path1 = FCPATH.'asset/hasil_resize/';

@unlink($path.$rowdel->namafile); 
@unlink($path1.$rowdel->namafile);

//Or better use ci file helper's `delete_files()` method

了解更多:https://www.codeigniter.com/user_guide/general/reserved_names.html