PBKK - Tugas 6

Membuat Fitur Upload File pada Template SBAdmin dengan CodeIgniter

Pada tugas sebelumnya, kita sudah menyelesaikan fitur CRUD tetapi untuk gambar produk belum ada sehingga akan ditampilkan broken image. Sehingga berikut ini adalah langkah-langkah untuk membuat fitur upload file:

1. Jika tidak ada gambar yang di-upload, maka akan diisi dengan nilai default.jpg. Caranya adalah buat folder baru bernama upload, lalu di dalamnya buat folder lagi bernama product. Pada folder product akan berisi file default.jpg. Berikut ini adalah gambar default.jpg:



2. Membuat Fitur Upload pada CodeIgniter:
  • Buka model Product_model.php, kemudian tambahkan method _uploadImage() tepat dibawah method delete(), isi dari kode method _uploadImage():
 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";
}
  • Ubah method save() dan update() pada file Product_model.php 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"];
        return $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"];
        return $this->db->update($this->_table, $this, array('product_id' => $post['id']));
    }
  • Menghapus file yang telah di-upload dengan cara menambahkan method _deleteImage() pada Product_model.php, isi dari kode _deleteImage() adalah:
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.*"));
    }
}
  • Panggil method _deleteImage() pada method delete(), seperti ini:
public function delete($id)
    {
        $this->_deleteImage($id);
        return $this->db->delete($this->_table, array("product_id" => $id));
    }
3. Hasil dari membuat fitur upload file:

  • Mengisi data tanpa ada gambar yang di-upload:

  • Mengisi data dan ada gambar yang di-upload:

  • Hasil yang terdapat di halaman list product, jika tidak meng-upload gambar maka akan muncul default.jpg tetapi jika meng-upload gambar maka akan muncul gambar yang kita upload, seperti ini:



  • Menghapus data yang sudah di-upload:




Comments

Popular Posts