2013-08-24 41 views
0

我認爲,上傳圖片和我喜歡的輸入類型的文件:CI中上傳圖片不能正常工作

<input type="file" name="ev_pic" size="20" /> 

和我的控制器,如:

if(strlen($_FILES['ev_pic']['name']) > 0) 
     { 
     $pic = $this->do_upload('ev_pic'); 
     } 

做上傳方法:

public function do_upload($field) { 
     $config['upload_path'] = './uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '1000'; 
     $config['max_width'] = '1024'; 
     $config['max_height'] = '768'; 

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

     if (!$this->upload->do_upload($field)) { 
      $error = array('error' => $this->upload->display_errors()); 

      return $error; 
     } else { 
      /*$data = array('upload_data' => $this->upload->data()); 
      return $data;*/ 
      $updata =$this->upload->data(); 
      $data = $updata['raw_name'].$updata['file_ext']; 
      return $data; 
     } 
    } 

在調試中顯示給我這個錯誤:

Severity: Notice 

Message: Undefined index: ev_pic 

Filename: administrator/events.php 

Line Number: 68 


Error Number: 1054 

Unknown column 'Array' in 'field list' 

UPDATE `events` SET `ev_id` = 0, `ev_text` = 0, `ev_pic` = Array 

Filename: D:\AppServ\www\d5n\rashaqa2\system\database\DB_driver.php 

Line Number: 330 

所以我會通過ev_pic命名文件,問題在哪裏?

+0

安置自己不上傳的方法 –

+0

可能重複 - (http://stackoverflow.com/questions/12769982/ [參考這個錯誤是什麼在PHP中呢?]參考這是什麼,這是錯誤的意思,在PHP) – hakre

+0

@userNOID我會編輯我的帖子 –

回答

0

請檢查您的形式

表單的enctype = 「的multipart/form-data的」 是avaliable與否

和改變這一行

如果(!$這個 - > upload-> do_upload( !$場)){到如果($這個 - > upload-> do_upload( 'ev_pic')){

+0

第一部分是,缺少'enctype'屬性可能是問題,但不需要更改代碼,'$ field'的值正確作爲參數傳遞給函數 –

1

負荷上傳庫你這樣的控制器的功能__construct:

public function __construct(){ 
    parent::__construct(); 
    $this->load->library('upload'); 
} 

然後改變你的函數如下:

public function do_upload($field) { 
     $config['upload_path'] = './uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '1000'; 
     $config['max_width'] = '1024'; 
     $config['max_height'] = '768'; 

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

     if (!$this->upload->do_upload($field)) { 
      $error = array('error' => $this->upload->display_errors()); 

      return $error; 
     } else { 
      /*$data = array('upload_data' => $this->upload->data()); 
      return $data;*/ 
      $updata =$this->upload->data(); 
      $data = $updata['raw_name'].$updata['file_ext']; 
      return $data; 
     } 
    }