2016-11-29 216 views
1

我想不僅在數據庫中,而且在文件夾中刪除圖像。在codeigniter中刪除後從文件夾中刪除圖像

這是我的模型

public function delete($id) 
     { 
      if ($this->db->delete("np_gallery", "id = ".$id)) 
      { 
      return true; 
      } 
     } 

這是我的控制器

public function delete_image($id) 
{ 
    $this->np_gallery_model->delete($id); 
    $query = $this->db->get("np_gallery"); 
    $data['records'] = $query->result(); 
    $this->load->view('admin/gallery/gallery_listing',$data); 
} 

這是我的看法

<table class="table table-bordered"> 
      <thead> 
       <tr> 
        <td>Sl No</td> 
        <td>Tag</td> 
        <td>Image</td> 
        <td>Action</td> 
       </tr> 
      </thead> 

      <?php 
      $SlNo=1; 
       foreach($records as $r) 
       { 
      ?> 
       <tbody> 
        <tr> 
        <?php $image_path = base_url().'uploads';?> 
        <td><?php echo $SlNo++ ; ?></td> 
        <td><?php echo $r->tag; ?></td> 
        <td><img src="<?php echo $image_path; ?>/images/gallery/<?php echo $r->picture;?>" style=" width:35%; height:100px;"/></td> 
        <td><a href="<?php echo site_url() . "/np_gallery/select_content/". $r->id?>" class="fa fa-pencil"></a>&nbsp;&nbsp; 
         <a href="<?php echo site_url() . "/np_gallery/delete_image/". $r->id?>" onClick="return confirm('Are you sure want to delete')" class="fa fa-trash"></a></td> 
        </tr> 
       </tbody> 
      <?php } ?> 
     </table> 

我在數據庫中刪除數據成功,但圖像在文件夾中也不會被刪除。

+0

<?php unlink(「image path」)?> –

+0

我應該在哪裏放這段代碼vivek –

+1

其中u是刪除控制器中的圖像u可應用像'if image delete than unlink(「imagepath」但你必須通過'圖像名稱'現在你只傳遞id或者你可以從控制器中獲取圖像名稱,無論你喜歡:) –

回答

1

添加一些額外的代碼在你的控制器:

public function delete_image($id) 
{ 
    $image_path = base_url().'uploads/images/gallery/'; // your image path 

    // get db record from image to be deleted 
    $query_get_image = $this->db->get_where('np_gallery', array('id' => $id)); 
    foreach ($query_get_image->result() as $record) 
    { 
     // delete file, if exists... 
     $filename = $image_path . $record->picture; 
     if (file_exists($filename)) 
     { 
      unlink($filename); 
     } 

     // ...and continue with your code 
     $this->np_gallery_model->delete($id); 
     $query = $this->db->get("np_gallery"); 
     $data['records'] = $query->result(); 
     $this->load->view('admin/gallery/gallery_listing',$data); 
    } 
} 

注:alternativelly,你可以做你的模型中刪除()方法來代替。考慮它更適合您的應用程序需求的地方。

相關問題