2017-01-31 51 views
1

我有inilabs學校管理腳本,工作正常。但我正在嘗試修改。我想在數據庫Codeigniter:如何用codeigniter上傳多個圖像

這裏上傳多張圖片是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>' 

上傳單個圖像這是控制器/ 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); 
    } 
} 

幫助我,請

+0

解決問題之前,我建議你使用$這個 - >輸入 - >後();爲您發佈的所有數據。您可以獲取所有發佈數據的數組,然後您可以添加特定鍵並添加特定值。 –

+0

[用codeigniter上傳多個圖像]可能的重複(http://stackoverflow.com/questions/33017753/upload-multiple-images-with-codeigniter) –

+0

此代碼工作正常。我可以上傳學生的照片。但我添加了更多上傳按鈕來上傳AADHAR CARD,BIRTH CERTIFICATE等。但我不知道如何編碼控制器。 – user7492700

回答

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;