如何在代码点火器框架中删除记录后从图像目录中删除图像


How to delete image from image directory after delete record in codeIgniter Framework?

我想删除一个或多个产品类别表单tbl_category。

  • 其中 category_id,category_name,...... ,category_image 是数据库字段。

  • 我的问题是,当我删除该项目时,它会完全删除从数据库中,但此图像仍保留在目录中

    我,e base_url.'。/images/category_images/'.

    我该怎么做?需要帮助。

这是控制器的代码:

      public function manage_product_category()
    {
        $data=array();
        $data['all_product_category']=  $this->ehome_model-  >select_all_product_category();
        $data['admin_maincontent']=$this->load->view('admin/view_product_category',$data,true);
        $this->load->view('admin/admin_master',$data);   
    }
    public function delete_product_category($category_id)
    {
        $this->super_a_model->delete_product_category_by_category_id($category_id);
        redirect('super_admin/manage_product_category');
    }

模型的以下代码:

     public function delete_product_category_by_category_id($category_id)
    {
        $this->db->where('category_id',$category_id);
        $this->db->delete('tbl_category');
    }

你需要使用取消链接的 php 方法: http://php.net/manual/it/function.unlink.php

$path = base_url.'./images/category_images/'.$file_name;
unlink($path);

确保设置正确的$file_name,如果没有,只需对数据库运行查询以获取文件名,然后再删除行。

检查此链接点击这里。

$this->load->helper("file");
delete_files($path);
首先,

您必须有一个img_url字段或任何根据您的要求在表中。然后在第一步中,您需要从表中获取该img_url,例如,然后像这样从目录中删除它,

//write this function in your_controller
function delete($your_id)
    {
        $img_url = $this->your_model->get_image_url($your_id);//this will get the img_url from your_ model 
        unlink($img_url);   //this will delete the image from your directory
        $this->your_model->delete_image_record($your_id); //this will delete the record from the table
    }
//write the following functions in your_model
function get_image_url($your_id)
    {
        $this->db->select('your_img_url');
        $this->db->from('your_table');
        $this->db->where('your_id',(int)$your_id);
        $query = $this->db->get();
        if($query->num_rows() == 1) return $query->row()->your_img_url;
        else return NULL;
    }
function delete_image_record($your_id)
    {
        $this->db->where("$your_id",(int)$your_id);
        $this->db->delete('your_table');
    }

在您的控制器中放置此代码。您需要从要删除的表中获取图像的路径。

$path_url = $this->your_model->get_path_url($your_id); 
if($path_url != NULL) 
{
    unlink($path_url ); //this will delete the image first.
    $this->your_model->delete_image_record($your_id);
}

并保留您的模型,只需编写一个额外的函数即可。以下函数将检索图像路径。

function get_path_url($your_id)
{
  $this->db->select('path_url');
  $this->db->from('your_table');
  $this->db->where('table_id',$your_id);
  $query = $this->db->get();
  if($query->num_rows() == 1) return $query->row()->path_url;
  else return NULL;
}