2016-08-26 53 views
0

我試圖將圖像上載到服務器上的文件夾,並將同一表單中的另一數據同時存儲在數據庫中。但它顯示錯誤您沒有選擇要上傳的文件無法使用Codeigniter上傳文件夾中的圖像

下面是我爲同一視圖和控制器:

查看:

<form role="form" method="post" action="job_insert" enctype="multipart/form-data"> 
         <div class="box-body"> 
          <div class="form-group"> 
           <label>Job Name*</label> 
           <input type="text" class="form-control" name="jname" placeholder="Enter Job Name" required> 
          </div> 

          <div class="form-group"> 
          <label>Description</label> 
          <textarea rows="4" cols="10" class="form-control" name="description" required></textarea> 
          </div> 
          <div class="form-group"> 
           <label>File input</label> 
          <input type="file" name="userimage"> 
          </div> 
          <div class="form-group"> 
         <label>Paper Size*</label> 

        <div class="form-group"> 
        <label>Paper Cutting Size</label> 
         <div> 
         <input id="cutting_size" name="cutting_size" type="text" placeholder="Cutting Size" class="form-control input-md" required> 
        </div> 
        </div> 
        <div class="form-group"> 
        <label>Sheet</label> 
        <div> 
         <input id="sheet" name="sheet" type="text" placeholder="No. of Sheet" class="form-control input-md" required> 
         </div> 
        </div> 
        <div class="form-group"> 
        <label>Tin No.</label> 
        <div> 
         <input id="sheet" name="tin" type="text" placeholder="Tin No." class="form-control input-md" required> 
         </div> 
        </div> 
        </div> 
      <!-- /.box-body --> 
        <div style="margin-left: 600px" class="box-footer"> 
         <button type="submit" class="btn btn-primary">Submit</button> 
        </div> 
       </form> 

控制器:

public function job_insert() 
     { 
      $this->form_validation->set_rules('jname', 'job_name', 'trim'); 
      if($this->form_validation->run()== FALSE) 
      { 
       $this->load->view('invalid_jobname'); 
      } 
      else 
      { 
       $this->do_upload(); 

       $data = $_SESSION['email']; 
       $id = $this->user_model->get_client_id($data); 
       $data = array('cid' => $id, 
           'job_name' => $this->input->post("jname"), 
           'job_id' => $this->input->post("job_id"), 
           'description' => $this->input->post("description"), 
           'img_name' => $img_name, 
           'adate' => Date('Y-m-d'), 
           'status' => 'NEW', 
           'paper_size' => $this->input->post("paper_size"), 
           'paper_type' => $this->input->post("paper_type"), 
           'cutting_size'=> $this->input->post("cutting_size"), 
           'sheet'=> $this->input->post("sheet"), 
           'tin' => $this->input->post("tin"), 
           'lamination' => $this->input->post("checkboxes"), 
           'print_type' => $this->input->post("checkboxes1"), 
           'binding' => $this->input->post("checkboxes2"), 
           ); 

       $query = $this->user_model->job_insert($data); 
       if ($query == TRUE) 
       { 
        $this->load->view('job_submitted'); 
       } 
       else 
       { 
        $this->load->view('error_page'); 
       } 

      } 
     } 

這是我的do_upload功能:

public function do_upload() 
    { 
    $config = array(
       'upload_path' => "./uploads/", 
       'allowed_types' => "gif|jpg|png|jpeg|pdf", 
       'overwrite' => FALSE, 
       'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb) 
       'max_height' => "768", 
       'max_width' => "1024" 
       ); 

       $this->load->library('upload', $config); 
       $image = $this->input->post("image"); 
       echo $image; 
       if(!$this->upload->do_upload('userimage')) 
       { 
        $error = $this->upload->display_errors(); 
        echo $error; 
       } 
     } 
+0

你錯過了'enctype'的形式,並給出了像'controller/method'方法的完整路徑 –

+0

我有添加enctype但它沒有工作 –

+0

更新你的代碼.. –

回答

0

提供enctype =「multipart/form-data」在您的表單標記中。沒有它,你不能上傳圖像。

+0

我剛剛嘗試過,但沒有奏效 –

0

與下面的代碼

<form role="form" method="post" action="job_insert" enctype="multipart/form-data"> 
1

更改此更新表單標籤

<input type="file" name="image"> 

<input type="file" name="userfile"/> 

和do_upload

if(!$this->upload->do_upload()) 

至 if(! $ this-> upload-> do_upload('userfile'))

+0

請更多詳細信息https://www.codeigniter.com/userguide3/libraries/file_uploading.html – Shibon

+0

太棒了,它很有效。非常感謝。你能給出一個理由說明爲什麼它沒有發生'形象'? –