2016-11-29 17 views
1

データベース内だけでなくフォルダ内にも画像を削除したいと思います。codeigniterで削除した後にフォルダから画像を削除

これはこれはこれは私がデータベース内のデータを削除することに成功しますが、画像私の見解

<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> 

ある

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

私のコントローラである

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

私のモデルであり、フォルダ内のファイルも削除されません。

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

注:

+0

<?php unlink( "image path")?> –

+0

このコードはどこに置くべきですか?vivek –

+1

ここでuはコントローラのイメージを削除しています。もし 'if image delete unlink(" imagepath ")'しかし、あなたはidだけを渡す 'image name'を渡す必要があります。あるいは、あなたが好きなものであれば、コントローラのidからイメージ名を得ることができます。 –

答えて

1

は、あなたのコントローラでいくつかの余分なコードを追加しますalternativelly、あなたの代わりにあなたのモデルが削除()メソッドの内部でそれを行うことができます。あなたの応募のニーズに適した場所を検討してください。

関連する問題