0

I'm trying to send a file from an HTML form to the CodeIgniter 3 Controller, but it shows an error message like this

Undefined Index : foto.

This is my HTML form script :

<div class="col-10">
<input type='file' id='foto1' name='foto[]' onchange='readURL(this);' onclick='tambahGambar(1)' multiple="">
</div>

I send it using ajax, like this :

 $.ajax({
                      url: '<?= base_url("admin/Masterbarang/addBarang"); ?>',
                      type: 'POST',
                      dataType: 'json',
                      data: $('#form-barang').serialize() + "&hpp=" + JSON.stringify(hpp) + "&rekomendasi=" + JSON.stringify(rekomendasi),
                      success: function(data) {
                          $("#nama-barang").attr("disabled", true)
                          console.log(data);

                      },
                      error: function(data) {
                          //   alert(data.message);
                          //   console.log(data.pesan_gagal);
                          $("#nama-barang").attr("disabled", true)
                          console.log(data);
                      }
                  });

In my controller received in this way :

 $number_of_files = sizeof($_FILES['foto']['tmp_name']);
        $foto = $_FILES['foto'];


        for ($i = 0; $i < $number_of_files; $i++) {
            $newName = $nama_barang . "_" . $i . ".png";
            if (!empty($foto['name'][$i])) {
                $_FILES['file']['name'] = $foto['name'][$i];
                $_FILES['file']['type'] = $foto['type'][$i];
                $_FILES['file']['tmp_name'] = $foto['tmp_name'][$i];
                $_FILES['file']['error'] = $foto['error'][$i];
                $_FILES['file']['size'] = $foto['size'][$i];

                $config['upload_path'] = FCPATH . 'assets/upload/product/';
                $config['allowed_types'] = 'jpg|jpeg|png|gif';
                $config['max_size'] = '5000';
                $config['file_name'] = $newName;

                $this->load->library('upload', $config);

                if ($this->upload->do_upload('file')) {
                    $uploadData = $this->upload->data();
                    $filename = $uploadData['file_name'];
                    $data['totalFiles'][] = $filename;
                }
            }
        }

Someone might be able to help me solve this problem

0 Answers0