Kamis, 05 Maret 2020

PBKK A - Tugas 6 - Penanganan File Upload

Nama: Ayu Mutiara Sari
NRP: 05111740000149

1. Pada tutorial sebelumnya, saat menambahkan produk, file foto yang dihasilkan adalah berupa broken image. ketika ditelusuri ternyata foto berada pada direktori /upload/product/ untuk itu maka kita harus membuat direktori  /upload/product/ 

2. Ketika mengupload produk tanpa foto, foto akan terisi dengan default.jpg. kita dapat mendownload file default.jpg pada default.jpg dan menaruhnya pada direktori /upload/product/ maka akan terlihat sebagai berikut:



3. Tetapi untuk produk dengan foto masih belum bisa. maka kita perlu menambahkan fitur upload image.Buka file Product_model.php, kemudian tambahkan method _uploadImage() tepat di bawah method delete().
 private function _uploadImage()  
 {  
   $config['upload_path']     = './upload/product/';  
   $config['allowed_types']    = 'gif|jpg|png';  
   $config['file_name']      = $this->product_id;  
   $config['overwrite']               = true;  
   $config['max_size']       = 1024; // 1MB  
   // $config['max_width']      = 1024;  
   // $config['max_height']      = 768;  
   $this->load->library('upload', $config);  
   if ($this->upload->do_upload('image')) {  
     return $this->upload->data("file_name");  
   }  
   return "default.jpg";  
 }  

4. lalu, ubah method  save() dan update() menjadi seperti ini:
 public function save()  
   {  
     $post = $this->input->post();  
     $this->product_id = uniqid();  
     $this->name = $post["name"];  
     $this->price = $post["price"];  
     $this->image = $this->_uploadImage();  
     $this->description = $post["description"];  
     $this->db->insert($this->_table, $this);  
   }  
   public function update()  
   {  
     $post = $this->input->post();  
     $this->product_id = $post["id"];  
     $this->name = $post["name"];  
     $this->price = $post["price"];  
     if (!empty($_FILES["image"]["name"])) {  
       $this->image = $this->_uploadImage();  
     } else {  
       $this->image = $post["old_image"];  
     }  
     $this->description = $post["description"];  
     $this->db->update($this->_table, $this, array('product_id' => $post['id']));  
   }  

Maka, ketika produk baru ditambah dengan foto, akan terlihat seperti berikut:

5. ketika produk dihapus, file didalam folder tidak terhapus, untuk menghapus file yang sudah dihapus, perlu menambahkan method _deleteImage() pada Product_model.


 private function _deleteImage($id)  
 {  
   $product = $this->getById($id);  
   if ($product->image != "default.jpg") {  
        $filename = explode(".", $product->image)[0];  
           return array_map('unlink', glob(FCPATH."upload/product/$filename.*"));  
   }  
 }  

lalu, tambahkan code pada method delete() untuk memanggil method _deleteImage() sehingga menjadi seperti:
 public function delete($id)  
   {  
     $this->_deleteImage($id);  
     return $this->db->delete($this->_table, array("product_id" => $id));  
   }  

0 komentar:

Posting Komentar