FFadhlan
fakhri007 4 tahun yang lalu
10.3k 2 CodeIgniter

Cara Delete / Unlink File Lama Dalam Folder Assets Codeigniter

Mohon bantuannya, setelah saya berhasil melakukan upload dan update gambar baru, file yang lama masih ada dalam folder assets padahal sudah pakai unlink tapi tetap gk ada efek di file tersebut ini Controllernya:

 public function det_dosen()
    {
        if($this->session->userdata('akses') =='1'){ 
            $id_login   = $this->input->post('id_login');
            $prodi    = $this->input->post('prodi');
            $jenjang  = $this->input->post('jenjang');
            $ttl      = $this->input->post('ttl');
            $email    = $this->input->post('email');
            $notelp   = $this->input->post('no_telp');
            $alamat   = $this->input->post('alamat');
            $foto = $_FILES['fotopost ']['name'];    
            $old = $this->input->post('fotolama');               

            if (empty($foto)){
                $config['upload_path']      = './assets/img/profile';
                $config['allowed_types']    = 'jpg|png|jpeg';
                $config['max_size']        = 2048;

                $this->load->library('upload',$config);
                $this->upload->initialize($config);
                if(!$this->upload->do_upload('fotopost')){
                    echo $this->upload->display_errors(); die();
                }
                else{
                    $foto = $this->upload->data('file_name');                
                }

                $data = array(      
                    'prodi'         => $prodi,
                    'jenjang_pend'  => $jenjang,
                    'alamat'        => $alamat,
                    'ttl'           => $ttl,
                    'no_telp'       => $notelp,
                    'email'         => $email,                                                                   
                    'profile'       => $foto 
                );               
                $this->m_admin->input_detdosen($id_login, $data);
            }
            else{
                $config['upload_path']      = './assets/img/profile';
                $config['allowed_types']    = 'jpg|png|jpeg';
                $config['max_size']        = 2048;

                $this->load->library('upload',$config);
                $this->upload->initialize($config);
                if(!$this->upload->do_upload('fotopost')){
                    echo $this->upload->display_errors(); die();
                }
                else{
                    $foto = $this->upload->data('file_name');                
                }

                $data = array(      
                    'prodi'         => $prodi,
                    'jenjang_pend'  => $jenjang,
                    'alamat'        => $alamat,
                    'ttl'           => $ttl,
                    'no_telp'       => $notelp,
                    'email'         => $email,                                                                   
                    'profile'       => $foto
                );
               $path = './assets/img/profile/'.$old;
                unlink($path);
                
                $this->m_admin->input_detdosen($id_login, $data);            
            }                      
            redirect('admin/show_dosen/'.$id_login);             
                                
        }else{
            redirect('login');
        }                                
    } 
Dan ini Model:

function input_detdosen($id_login, $data){	
        $this->db->where('id_login',$id_login);
        return $this->db->update('data_dosen', $data);  
    }
Input di index:

<input type="hidden" name="fotolama" value="<?php echo $ds['profile'];?>">
<input type="file" name="fotopost" class="form-control">

2 Jawaban

Jhony Code
daycode4 tahun yang lalu
coba kasih chmod dulu file yang mau dihapus misalnya

$path = './assets/img/profile/'.$old;
chmod($path, 0777);
unlink($path);
0
arif
echooff3 tahun yang lalu
dia gak mau delete karna ada spasi saat unlink coba di hilangin dulu pake trim ini jalan kalau buat saya , codeigniter 3

$old = trim($this->input->post('fotolama'));
1
Silahkan Login untuk menjawab.

Top Coder