如何在Codeigniter中发送多个文件上传?

时间:2019-07-09 08:10:19

标签: php mysql codeigniter

我想插入带有多个文件的数据,但是如果我插入数据库,则该文件为null。这是我的代码

从动作代码


    <form action="<?php echo base_url();?>SystemCore/tambahKendaraan" method="POST" enctype="multipart/form-data">
    <div class="form-group">
        <label>Foto Foto Kendaraan</label>
        <input type="file" class="form-control" multiple="" required="" name="addKendaraanDinasFoto[]">
        <small>*Dapat Mencantumkan Foto Lebih Dari 1</small>
    </div>
    <button type="submit" class="btn btn-success">Simpan</button>
    </form>

这是我的控制器要上传的数据


    public function tambahKendaraan()
    {
        $simpanData = array(
            "id_jenis" => $this->input->post("addKendaraanDinasJenis"),
            "plat_kendaraan" => $this->input->post("addKendaraanDinasPlat"),
            "dinas_pengelola" => $this->input->post("addKendaraanDinasPengelola"),
            "pemegang_kendaraan" => $this->input->post("addKendaraanDinasPemegang"),
            "no_stnk" => $this->input->post("addKendaraanDinasSTNK"),
            "no_rangka" => $this->input->post("addKendaraanDinasRangka"),
            "masa_pajak" => $this->input->post("addKendaraanDinasPajak"),
            "merk_kendaraan" => $this->input->post("addKendaraanDinasMerk"),
            "warna_kendaraan" => $this->input->post("addKendaraanDinasWarna"),
            "status_kendaraan" => $this->input->post("addKendaraanDinasStatus"),
        );
            if ($this->db->insert("tbl_mobil", $simpanData)) {

        $insert_id = $this->db->insert_id();
        date_default_timezone_get("Asia/Jakarta");

        $this->load->library('upload');
        $dataInfo = array();
        $files = $_FILES;
        $cpt = count($_FILES['addKendaraanDinasFoto']['name']);

        for($i=0; $i<$cpt; $i++)
        {         
            $_FILES['addKendaraanDinasFoto']['name']= $files['addKendaraanDinasFoto']['name'][$i];
            $_FILES['addKendaraanDinasFoto']['type']= $files['addKendaraanDinasFoto']['type'][$i];
            $_FILES['addKendaraanDinasFoto']['tmp_name']= $files['addKendaraanDinasFoto']['tmp_name'][$i];
            $_FILES['addKendaraanDinasFoto']['error']= $files['addKendaraanDinasFoto']['error'][$i];
            $_FILES['addKendaraanDinasFoto']['size']= $files['addKendaraanDinasFoto']['size'][$i];

            $this->upload->initialize($this->set_upload_options());
            $this->upload->do_upload();
            $dataInfo[] = $this->upload->data();

            $this->db->insert("tbl_fotomobil", $data);
        }


        foreach ($dataInfo as $infos) {
            $data = array(
                'id_mobil' => $insert_id,
                'tgl_foto' => date('Y-m-d H:i:s'),
                'foto_mobil' => $infos['file_name'],
            );
        }

               $this->session->set_flashdata('notif', '<script>swal ( "Success" , "Data Kendaraan Berhasil Ditambahkan !" ,  "success" );</script>');
            }else{
                $this->session->set_flashdata('notif', '<script>swal ( "Error" ,  "Data Error !" ,  "error" );</script>');
            }
                redirect('kendaraan');
    }
    private function set_upload_options()
    {   
        $config = array();
        $config['upload_path']   = './upload/kendaraan/';
        $config['allowed_types'] = 'jpg|png|jpeg|svg';
        $config['max_size']      = 10097152;
        $config['overwrite']     = FALSE;

        return $config;
    }

结果:


    array(3) {
      ["id_mobil"]=>
      int(0)
      ["tgl_foto"]=>
      string(19) "2019-07-09 09:55:58"
      ["foto_mobil"]=>
      string(0) ""
    }
    array(3) {
      ["id_mobil"]=>
      int(0)
      ["tgl_foto"]=>
      string(19) "2019-07-09 09:55:58"
      ["foto_mobil"]=>
      string(0) ""
    }

1 个答案:

答案 0 :(得分:0)

<div class="main-container">
  <div class="mynav" id="mynav">
  Hello World! this is mynav
  </div>
</div>