How to delete images from folder in Codeigniter

php codeigniter gallery

3718 просмотра

4 ответа

1 Репутация автора

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

Автор: aTypicalStuden Источник Размещён: 18.07.2016 06:45

Ответы (4)


0 плюса

610 Репутация автора

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');
}
Автор: Rijin Размещён: 18.07.2016 07:07

1 плюс

389 Репутация автора

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');
}
Автор: smokehill Размещён: 18.07.2016 07:42

0 плюса

48 Репутация автора

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

    unlink(FCPATH.$image->file);

}

Автор: Jitendra Y Размещён: 18.07.2016 09:17

0 плюса

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"));
}
Автор: Rizqi Mauludin Размещён: 21.11.2016 03:15
Вопросы из категории :
32x32