在codeigniter中上传2个或更多具有不同字段名称和不同输入的图像

时间:2017-06-23 08:49:24

标签: php codeigniter upload

我想为每张图片上传多个具有不同输入和不同字段名称的图像。我实际上尝试用两种不同的方法将它分开,比如

 public function tambahketua(){
     if(isset($_POST["reg"]))
        {

            $config['upload_path'] = './asset/img/foto/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|doc|xml|docx|GIF|JPG|PNG|JPEG|PDF|DOC|XML|DOCX|xls|xlsx';
            $config['max_size'] = 200048;
            $config['file_name'] = $this->input->post("namaketua");

            $this->upload->initialize($config);

            if ( ! $this->upload->do_upload('attachment'))//ini name di input choose file
            {

            }
            else
            {
            $datas = $this->upload->data();
            $edit['image_library'] = 'gd2';
            $edit['source_image']   = './asset/img/foto/'.$datas['file_name'];
            $edit['create_thumb'] = TRUE;
            $edit['maintain_ratio'] = TRUE;
            $edit['height'] = 600;
            $this->load->library('image_lib', $edit);
            $this->image_lib->resize();
            $edit_file = explode('.', $datas['file_name']);
            $data['foto'] = $edit_file[0].'_thumb.'.$edit_file[1];
            unlink($config['upload_path'].$datas['file_name']);
            }


            $a = $this->miniatur_model->insertPeserta($data);
            if($a){
                $this->tambahanggota1();
            }
            else{
                echo "failed";
            }

    }
        $this->load->view('pendaftaran');
}


 public function tambahanggota1(){
    if(isset($_POST["reg"]))
        {
            $config['upload_path'] = './asset/img/foto/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|doc|xml|docx|GIF|JPG|PNG|JPEG|PDF|DOC|XML|DOCX|xls|xlsx';
            $config['max_size'] = 200048;
            $config['file_name'] = $this->input->post("namaanggota1");
            $this->upload->initialize($config);

            if ( ! $this->upload->do_upload('fotoktmanggota1'))//ini name di input choose file
            {

            }
            else
            {
            $datas = $this->upload->data();
            $edit['image_library'] = 'gd2';
            $edit['source_image']   = './asset/img/foto/'.$datas['file_name'];
            $edit['create_thumb'] = TRUE;
            $edit['maintain_ratio'] = TRUE;
            $edit['height'] = 600;
            $this->load->library('image_lib', $edit);
            $this->image_lib->resize();
            $edit_file = explode('.', $datas['file_name']);
            $data['foto'] = $edit_file[0].'_thumb.'.$edit_file[1];
            unlink($config['upload_path'].$datas['file_name']);
            }

            $a = $this->miniatur_model->insertPeserta($data);
            if($a){
                echo "success";
            }
            else{
                echo "failed";
            }
    }        
    $this->load->view('pendaftaran');
}

到目前为止我只能将两个图像名称都写入我的数据库,但只有第一个上传到我的文件夹中。 这是我的看法

<input type="file" id="attachment" name="attachment"> <input type="file" id="fotoktmanggota1" name="fotoktmanggota1">

由于某种原因,我无法将我的文件名称更改为name="attachment[]"

等数组

1 个答案:

答案 0 :(得分:0)

我已将您的额外代码最小化。试试这个,如果我错过了什么就做你自己的魔法。

[]