在codeigniter中删除后从文件夹中删除图像 [英] Remove image from folder after deleting in codeigniter

查看:42
本文介绍了在codeigniter中删除后从文件夹中删除图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不仅要删除数据库中的图像,还要删除文件夹中的图像。

I want to delete the image not only in database, but in folder too.

这是我的模型

    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>

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

I succeed in deleting the data in the database, but the image in the folder are not also be deleted.

推荐答案

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

Add some extra code in your controller:

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()方法。考虑哪里最适合您的应用需求。

Note: alternativelly, you can do it inside your model delete() method instead. Consider where it better fits your applicaction needs.

这篇关于在codeigniter中删除后从文件夹中删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆