【问题标题】:How to delete image from image directory after delete record in codeIgniter Framework?在codeIgniter Framework中删除记录后如何从图像目录中删除图像?
【发布时间】:2015-02-14 14:16:14
【问题描述】:

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

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

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

    i,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');
    }

【问题讨论】:

  • 请格式化您的帖子以从代码块中排除常规文本。只有代码应该放在代码块中。
  • 重定向前...您可以从数据库中获取数据(删除前),并在您的网络空间中取消设置文件

标签: javascript php codeigniter image-uploading


【解决方案1】:

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

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

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

【讨论】:

  • 亲爱的 christian giupponi 我在哪里写你的代码。请控制器或模型提供帮助。
  • 在控制器中,删除之前的类别
【解决方案2】:

检查此链接 click here.

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

【讨论】:

  • 亲爱的parth 我在哪里写你的代码。控制器或模型。
  • 在控制器中添加方法
【解决方案3】:

首先,您的表中必须有一个 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');
    }

【讨论】:

    【解决方案4】:

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

    $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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      • 2013-07-15
      • 1970-01-01
      相关资源
      最近更新 更多