如何在CodeIgniter中已声明的if和for语句之外声明变量

时间:2019-04-03 03:44:23

标签: php codeigniter upload updating

我正在尝试用新的图像表更新。

这是我的控制者:

        if(!empty($_FILES['gambar']['name'])){
            $filesNumber        =   sizeof($_FILES['gambar']['tmp_name']);
            $files          =   $_FILES['gambar'];
            $config['upload_path']          = './assets/img/pics/';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 2048;
            $config['encrypt_name']      = true;

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

                $this->upload->initialize($config);
                if($this->upload->do_upload('gambar')){
                    $data = $this->upload->data();

                    $insert[$i]['kode_asset']   = $this->input->post('kode');
                    $insert[$i]['gambar']       = $data['file_name'];
                }
            }

        $this->db->insert_batch('gambar', $insert);
        redirect('admin/asset','refresh');
        }

这是多个文件的输入:

<input type="file" name="gambar[]" id="gambar" class="form-control-file" multiple>

但是它会以Undefined variable: insert语法发出insert_batch通知,并且每次都会出现You must use the "set" method to update an entry. db错误。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

检查语句:

// check filesNumber value, after see this , you may fix this quickly.
 $filesNumber        =   sizeof($_FILES['gambar']['tmp_name']);