How to delete images from folder in Codeigniter
<p>Today My teacher in my college give me a task about CodeIgniter, he gives me this Controller </p>

<pre><code>public function __construct()
{
    parent::__construct();
    $this-&gt;load-&gt;model('Gallery_model');
    $this-&gt;load-&gt;helper(['url','html','form']);
    $this-&gt;load-&gt;database();
    $this-&gt;load-&gt;library(['form_validation','session','image_lib']);
}

public function index()
{
    $data = ['images' =&gt; $this-&gt;Gallery_model-&gt;all()];
    $this-&gt;load-&gt;view($this-&gt;layoutgaleri, $data);
}

public function add(){
    $rules =    [
                    [
                            'field' =&gt; 'caption',
                            'label' =&gt; 'Caption',
                            'rules' =&gt; 'required'
                    ],
                    [
                            'field' =&gt; 'description',
                            'label' =&gt; 'Description',
                            'rules' =&gt; 'required'
                    ]
                ];

    $this-&gt;form_validation-&gt;set_rules($rules);

    if ($this-&gt;form_validation-&gt;run() == FALSE)
    {
        $this-&gt;load-&gt;view('admin/layoutgaleriadd');
    }
    else
    {

        /* Start Uploading File */
        $config =   [
                        'upload_path'   =&gt; './asset/images/',
                        'allowed_types' =&gt; 'gif|jpg|png',
                        'max_size'      =&gt; 2000,
                        'max_width'     =&gt; 640,
                        'max_height'    =&gt; 480
                    ];

        $this-&gt;load-&gt;library('upload', $config);

        if ( ! $this-&gt;upload-&gt;do_upload())
        {
                $error = array('error' =&gt; $this-&gt;upload-&gt;display_errors());

                $this-&gt;load-&gt;view('admin/layoutgaleriadd', $error);
        }
        else
        {
                $file = $this-&gt;upload-&gt;data();
                //print_r($file);
                $data = [
                            'file'          =&gt; 'asset/images/' . $file['file_name'],
                            'caption'       =&gt; set_value('caption'),
                            'description'   =&gt; set_value('description')
                        ];
                $this-&gt;Gallery_model-&gt;create($data);
                $this-&gt;session-&gt;set_flashdata('message','Gambar sudah ditambahkan..');
                redirect('admin/Galeri');
        }
    }
}

public function edit($id){
    $rules =    [
                    [
                            'field' =&gt; 'caption',
                            'label' =&gt; 'Caption',
                            'rules' =&gt; 'required'
                    ],
                    [
                            'field' =&gt; 'description',
                            'label' =&gt; 'Description',
                            'rules' =&gt; 'required'
                    ]
                ];

    $this-&gt;form_validation-&gt;set_rules($rules);
    $image = $this-&gt;Gallery_model-&gt;find($id)-&gt;row();

    if ($this-&gt;form_validation-&gt;run() == FALSE)
    {
        $this-&gt;load-&gt;view('admin/layoutgaleriedit',['image'=&gt;$image]);
    }
    else
    {
        if(isset($_FILES["userfile"]["name"]))
        {
            /* Start Uploading File */
            $config =   [
                            'upload_path'   =&gt; './asset/images/',
                            'allowed_types' =&gt; 'gif|jpg|png',
                            'max_size'      =&gt; 2000,
                            'max_width'     =&gt; 640,
                            'max_height'    =&gt; 480
                        ];

            $this-&gt;load-&gt;library('upload', $config);

            if ( ! $this-&gt;upload-&gt;do_upload())
            {
                    $error = array('error' =&gt; $this-&gt;upload-&gt;display_errors());
                    $this-&gt;load-&gt;view('admin/layoutgaleriedit',['image'=&gt;$image,'error'=&gt;$error]);
            }
            else
            {
                    $file = $this-&gt;upload-&gt;data();
                    $data['file'] = 'asset/images/' . $file['file_name'];
                    unlink($image-&gt;file);
            }
        }

        $data['caption']        = set_value('caption');
        $data['description']    = set_value('description');

        $this-&gt;Gallery_model-&gt;update($id, $data);
        $this-&gt;session-&gt;set_flashdata('message','Gambar sudah diperbarui..');
        redirect('admin/galeri');
    }
}


public function delete($id)
{
    $this-&gt;Gallery_model-&gt;delete($id);
    $this-&gt;session-&gt;set_flashdata('message','Gambar sudah dihapus..');
    redirect('admin/galeri');
}
</code></pre>

<p>The problem is we had to create a model that handle function delete, so it can delete a photo from the folder too, here it's the model</p>

<pre><code>public function delete($id)
{
    try {
        $this-&gt;db-&gt;where('id',$id)-&gt;delete('tb_gambar');
        return true;
    }
    //catch exception
    catch(Exception $e) {
      echo $e-&gt;getMessage();
    }
}
</code></pre>

<p>He gives us a hint to use unlink, but I don't have any idea how to use it, so I try to ask you all for the answer. I hope you all can show me the right answer for my school task</p>