在codeigniter中删除后从文件夹中删除图像

时间:2016-11-29 10:02:56

标签: php 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>

我成功删除了数据库中的数据,但文件夹中的图像也不会被删除。

2 个答案:

答案 0 :(得分:2)

在控制器中添加一些额外的代码:

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

注意:另外,您可以在模型delete()方法中执行此操作。考虑哪种方式更适合您的应用需求。

答案 1 :(得分:1)

试试这些

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="" method="POST">
  <table class="table table-hover my-5">
    <thead class="">
      <tr>
        <th>No</th>
        <th>Name</th>
        <th>rank</th>
        <th>gmail</th>
        <th>Delete?</th>
      </tr>
    </thead>
    <tbody class="my_dynamic_table">
      <tr>
        <td>1</td>
        <td><input type="text" name="name" value="{{ i.name }}"></td>
        <td><input type="text" name="rank" value="{{ i.rank }}"></td>
        <td><input type="email" name="gmail" value="s@gmail.com"></td>
        <td><i class="fa fa-times-circle remove" style="font-size: 22px; color: red;">x</i></td>
      </tr>
    </tbody>
  </table>
  <div class="row mx-5">
    <button class="btn btn-warning add" type="button">Add row</button>
    <button type="Submit" class="btn btn-primary ml-auto">Submit</button>
  </div>
</form>