2016-01-13 63 views
0

我試圖上傳一個使用codeigniter的圖像,不管是什麼 我總是收到「您沒有選擇要上傳的文件」錯誤。Codeigniter:您沒有選擇要上傳的文件

這裏是我的控制器:

<?php 
    class Post extends CI_Controller { 
     function __construct(){ 
      parent::__construct(); 
     } 

     function index(){ 
      $this->load->view('upload_form', array('error' => '')); 
     } 

     function upload(){   
      $config['upload_path'] = './uploads/'; 
      $config['allowed_types'] = 'gif|jpg|jpeg|png'; 
      $config['max_size'] = '1024'; 
      $config['max_width'] = '1024'; 
      $config['max_height'] = '1024'; 

      $this->load->library('upload', $config); 

      if (!$this->upload->do_upload('userfile')) 
      { 
       $error = array('error' => $this->upload->display_errors()); 
       echo json_encode($error); 
      } 
      else 
      { 
       $file_data = array('upload_data' => $this->upload->data()); 
       $data['img'] = base_url().'/images/'.$file_data['file_name']; 
       echo json_encode($data); 
      } 
    } 
    } 
?> 

這是我的看法:

<html lang="en"> 
    <head> 
     <meta charset="utf-8"/> 
     <meta http-equiv="X-UA-Compatible" content="IE-9"/> 
    </head> 
    <body> 

    <form action="" method="POST" enctype="multipart/form-data" > 
    Select File To Upload:<br /> 
    <input type="file" name="userfile" /> 
    <br /><br /> 
    <input type="submit" name="submit" value="Upload" class="btn btn-success" /> 
</form> 
    </body> 
</html> 

誰能幫我解決這個問題呢?我感謝所有幫助:)

回答

-1

試試這個

$image=$this->input->post('userfile'); // getting your posted image here 

     if (!$this->upload->do_upload($image)) //passing your image to upload 
     { 
      $error = array('error' => $this->upload->display_errors()); 
      echo json_encode($error); 
     } 
     else 
     { 
      $file_data = array('upload_data' => $this->upload->data()); 
      $data['img'] = base_url().'/images/'.$file_data['file_name']; 
      echo json_encode($data); 
     } 
+3

這是一個文件不是一個帖子'$圖像= $這個 - >輸入 - >後( 'userfile的');' – user4419336

0

上工作,你要提交表單,按代碼索引功能加載形式,你在上傳功能代替提交同一頁上

相關問題