Untitled

mail@pastecode.io avatar
unknown
php
2 years ago
2.2 kB
4
Indexable
Never
public function proses_upload()
    {
        // Panggil Model M_Welcome
        $this->load->model('Upload_image_model');

        // Hitung Jumlah File/Gambar yang dipilih
        $jumlahData = count($_FILES['gambar']['name']);


        // Lakukan Perulangan dengan maksimal ulang Jumlah File yang dipilih
        for ($i = 0; $i < $jumlahData; $i++) :

            // Inisialisasi Nama,Tipe,Dll.
            $_FILES['file']['name']     = $_FILES['gambar']['name'][$i];
            $_FILES['file']['type']     = $_FILES['gambar']['type'][$i];
            $_FILES['file']['tmp_name'] = $_FILES['gambar']['tmp_name'][$i];
            $_FILES['file']['size']     = $_FILES['gambar']['size'][$i];

            // Konfigurasi Upload
            $config['upload_path']          = './assets/img/kpi/';
            $config['allowed_types']        = 'gif|jpg|png|pdf|jpeg';
            $tanggal = $this->input->post('month');
            $departemen = $this->input->post('departemen');
            // $config['file_name'] = $new_name;
            // Memanggil Library Upload dan Setting Konfigurasi
            $this->load->library('upload', $config);
            $this->upload->initialize($config);

            if ($this->upload->do_upload('file')) { // Jika Berhasil Upload

                $fileData = $this->upload->data(); // Lakukan Upload Data

                // Membuat Variable untuk dimasukkan ke Database
                $uploadData[$i]['gambar'] =  $tanggal . '-' . $departemen;
                $uploadData[$i]['month'] = $this->input->post('month');
                $uploadData[$i]['departemen'] = $this->input->post('departemen');
            }

        endfor; // Penutup For
        // echo "<pre>";
        // print_r($uploadData);
        // echo "</pre>";
        // die();

        if ($uploadData !== null) { // Jika Berhasil Upload

            // Insert ke Database 
            $insert = $this->Upload_image_model->upload($uploadData);

            if ($insert) { // Jika Berhasil Insert
                redirect('Home_controller/showData');
            } else { // Jika Tidak Berhasil Insert
                redirect('Home_controller/upload');
            }
        }
    }