Codeigniter:如何使用codeigniter上传多个图像

时间:2017-01-31 01:42:25

标签: php mysql codeigniter codeigniter-upload

我有inilabs学校管理脚本,工作正常。但我正在尝试修改。我想在数据库中上传多个图像

这是add.php

' <?php 
                    if(isset($image)) 
                        echo "<div class='form-group has-error' >";
                    else     
                        echo "<div class='form-group' >";
                ?>
                    <label for="photo" class="col-sm-2 control-label col-xs-8 col-md-2">
                        <?=$this->lang->line("student_photo")?>
                    </label>
                    <div class="col-sm-4 col-xs-6 col-md-4">
                        <input class="form-control"  id="uploadFile1" placeholder="Choose File" disabled />  
                    </div>

                    <div class="col-sm-2 col-xs-6 col-md-2">
                        <div class="fileUpload btn btn-success form-control">
                            <span class="fa fa-repeat"></span>
                            <span><?=$this->lang->line("upload")?></span>
                            <input id="uploadBtn1" type="file" class="upload" name="image" />
                        </div>
                    </div>
                     <span class="col-sm-4 control-label col-xs-6 col-md-4">

                        <?php if(isset($image)) echo $image; ?>
                    </span>
                </div>

                <?php 
                    if(isset($imageaadhar)) 
                        echo "<div class='form-group has-error' >";
                    else     
                        echo "<div class='form-group' >";
                ?>
                    <label for="aadhar" class="col-sm-2 control-label col-xs-8 col-md-2">
                        <?=$this->lang->line("student_aadhar")?>
                    </label>
                    <div class="col-sm-4 col-xs-6 col-md-4">
                        <input class="form-control"  id="uploadFile2" placeholder="Choose File" disabled />  
                    </div>

                    <div class="col-sm-2 col-xs-6 col-md-2">
                        <div class="fileUpload btn btn-success form-control">
                            <span class="fa fa-repeat"></span>
                            <span><?=$this->lang->line("upload")?></span>
                            <input id="uploadBtn2" type="file" class="upload" name="imageaadhar" />
                        </div>
                    </div>
                     <span class="col-sm-4 control-label col-xs-6 col-md-4">

                        <?php if(isset($imageaadhar)) echo $imageaadhar; ?>
                    </span>
                </div>

                <?php 
                    if(isset($imagebirthc)) 
                        echo "<div class='form-group has-error' >";
                    else     
                        echo "<div class='form-group' >";
                ?>
                    <label for="birthc" class="col-sm-2 control-label col-xs-8 col-md-2">
                        <?=$this->lang->line("student_birthc")?>
                    </label>
                    <div class="col-sm-4 col-xs-6 col-md-4">
                        <input class="form-control"  id="uploadFile3" placeholder="Choose File" disabled />  
                    </div>

                    <div class="col-sm-2 col-xs-6 col-md-2">
                        <div class="fileUpload btn btn-success form-control">
                            <span class="fa fa-repeat"></span>
                            <span><?=$this->lang->line("upload")?></span>
                            <input id="uploadBtn3" type="file" class="upload" name="image" />
                        </div>
                    </div>
                     <span class="col-sm-4 control-label col-xs-6 col-md-4">

                        <?php if(isset($imagebirthc)) echo $imagebirthc; ?>
                    </span>
                </div>'

这是上传单张图片的controller / add.php。如何修改上传两个以上的图片?

        $classesID = $this->input->post("classesID");

        if($classesID != 0) {
            $this->data['sections'] = $this->section_m->get_order_by_section(array("classesID" =>$classesID));
        } else {
            $this->data['sections'] = "empty";
        }
        $this->data['sectionID'] = $this->input->post("sectionID");

        if($_POST) {
            $rules = $this->rules();
            $this->form_validation->set_rules($rules);
            if ($this->form_validation->run() == FALSE) {
                $this->data["subview"] = "student/add";
                $this->load->view('_layout_main', $this->data);
            } else {

                $sectionID = $this->input->post("sectionID");
                if($sectionID == 0) {
                    $this->data['sectionID'] = 0;
                } else {
                    $this->data['sections'] = $this->section_m->get_allsection($classesID);
                    $this->data['sectionID'] = $this->input->post("sectionID");
                }

                $dbmaxyear = $this->student_m->get_order_by_student_single_max_year($classesID);
                $maxyear = "";
                if(count($dbmaxyear)) {
                    $maxyear = $dbmaxyear->year;
                } else {
                    $maxyear = date("Y");
                }

                $section = $this->section_m->get_section($sectionID);
                $array = array();
                $array["name"] = $this->input->post("name");
                $array["dob"] = date("Y-m-d", strtotime($this->input->post("dob")));
                $array["sex"] = $this->input->post("sex");
                $array["religion"] = $this->input->post("religion");
                $array["email"] = $this->input->post("email");
                $array["phone"] = $this->input->post("phone");
                $array["address"] = $this->input->post("address");
                $array["classesID"] = $this->input->post("classesID");
                $array["sectionID"] = $this->input->post("sectionID");
                $array["section"] = $section->section;
                $array["roll"] = $this->input->post("roll");
                $array["username"] = $this->input->post("username");
                $array['password'] = $this->student_m->hash($this->input->post("password"));
                $array['usertype'] = "Student";
                $array['parentID'] = $this->input->post('guargianID');
                $array['library'] = 0;
                $array['hostel'] = 0;
                $array['transport'] = 0;
                $array['create_date'] = date("Y-m-d");
                $array['year'] = $maxyear;
                $array['totalamount'] = 0;
                $array['paidamount'] = 0;
                $array["create_date"] = date("Y-m-d h:i:s");
                $array["modify_date"] = date("Y-m-d h:i:s");
                $array["create_userID"] = $this->session->userdata('loginuserID');
                $array["create_username"] = $this->session->userdata('username');
                $array["create_usertype"] = $this->session->userdata('usertype');
                $array["studentactive"] = 1;




                $new_file = "";
                if($_FILES["image"]['name'] !="") {
                    $file_name = $_FILES["image"]['name'];
                    $file_name_rename = $this->insert_with_image($this->input->post("username"));
                    $explode = explode('.', $file_name);
                    if(count($explode) >= 2) {

                        $new_file = $file_name_rename.'.'.$explode[1];
                        $config['upload_path'] = "./uploads/images";
                        $config['allowed_types'] = "gif|jpg|png";
                        $config['file_name'] = $new_file;
                        $config['max_size'] = '1024';
                        $config['max_width'] = '3000';
                        $config['max_height'] = '3000';
                        $array['photo'] = $new_file;
                        $this->load->library('upload', $config);
                        if(!$this->upload->do_upload("image")) {
                            $this->data["image"] = $this->upload->display_errors();
                            $this->data["subview"] = "student/add";
                            $this->load->view('_layout_main', $this->data);
                        } else {
                            $data = array("upload_data" => $this->upload->data());
                            $this->student_m->insert_student($array);
                            $this->session->set_flashdata('success', $this->lang->line('menu_success'));
                            redirect(base_url("student/index"));
                        }
                    } else {
                        $this->data["image"] = "Invalid file";
                        $this->data["subview"] = "student/add";
                        $this->load->view('_layout_main', $this->data);
                    }
                } else {
                    $array["photo"] = $new_file;
                    $this->student_m->insert_student($array);
                    $this->session->set_flashdata('success', $this->lang->line('menu_success'));
                    redirect(base_url("student/index"));
                }
            }
        } else {
            $this->data["subview"] = "student/add";
            $this->load->view('_layout_main', $this->data);
        }
    } else {
        $this->data["subview"] = "error";
        $this->load->view('_layout_main', $this->data);
    }
}

请帮帮我

2 个答案:

答案 0 :(得分:0)

    your file name 
    <input id="uploadBtn3" type="file" class="upload" name="image[]" />

in controller


    $files = $_FILES;
            $count = count($_FILES['uploadfile']['name']);
            for($i=0; $i<$count; $i++)
                    {
                    $_FILES['uploadfile']['name']= $files['uploadfile']['name'][$i];
                    $_FILES['uploadfile']['type']= $files['uploadfile']['type'][$i];
                    $_FILES['uploadfile']['tmp_name']= $files['uploadfile']['tmp_name'][$i];
                    $_FILES['uploadfile']['error']= $files['uploadfile']['error'][$i];
                    $_FILES['uploadfile']['size']= $files['uploadfile']['size'][$i];
                    $this->upload->initialize($this->set_upload_options());//function defination below
                    $this->upload->do_upload('uploadfile');
                    $upload_data = $this->upload->data();
                    $name_array[] = $upload_data['file_name'];
                    $fileName = $upload_data['file_name'];
                    $image[] = $fileName;

                    }
                  $fileName = $image;

答案 1 :(得分:0)

使用库Codeigniter(library upload single file and multi file)轻松上传