aTypicalStuden
aTypicalStuden

Reputation: 1

How to delete images from folder in Codeigniter

Today My teacher in my college give me a task about CodeIgniter, he gives me this Controller

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

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

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

    $this->form_validation->set_rules($rules);

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

Upvotes: 0

Views: 5366

Answers (4)

blues911
blues911

Reputation: 910

You can delete image from DB and folder in the one function:

public function delete($id)
{
    $image = $this->Gallery_model->find($id)->row();

    // delete image
    if (is_file('image_path/'.$image->file)) {
        unlink('image_path/'.$image->file);
    }

    $this->Gallery_model->delete($id);
    $this->session->set_flashdata('message','Gambar sudah dihapus..');

    redirect('admin/galeri');
}

Upvotes: 1

Rizqi Mauludin
Rizqi Mauludin

Reputation: 1

here is my delete function and it works

public function delete($id){
$image = $this->M_sizechart->find($id)->row();
 $this->M_sizechart->delete($id);
 if(file_exists(FCPATH.$image->file)) {
   unlink(FCPATH.$image->file);
 }
 $this->session->set_flashdata('message','Image has been deleted..');
 redirect(base_url("sizechart"));
}

Upvotes: 0

Jitendra Y
Jitendra Y

Reputation: 25

if(file_exists(FCPATH.$image->file)) {

    unlink(FCPATH.$image->file);

}

Upvotes: 0

Rijin
Rijin

Reputation: 924

in controller :

public function delete($id)
{
    $image = $this->Gallery_model->find($id)->row();
    $this->Gallery_model->delete($id);
    // use
    delete_files(FCPATH.$image->file);  // codeigniter method to delete file
    // or use php method
    // unlink(FCPATH.$image->file);  // php unlink method
    $this->session->set_flashdata('message','Gambar sudah dihapus..');
    redirect('admin/galeri');
}

Upvotes: 0

Related Questions