2012-11-07 51 views
2

這是控制器:上傳圖片的路徑進入該文件夾的名稱數據庫,並上傳圖片

public function category() 
    { 
     if($this->form_validation->run()==FALSE) 
     { 
      $this->load->view('admin/home'); 
     } 
     else{ 
       $config['upload_path'] = 'uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '100'; 
     $config['max_width'] = '1024'; 
     $config['max_height'] = '768'; 
       $this->load->library('upload', $config); 
       $image_data=$_POST['file']; 
      if (! $this->upload->do_upload()) 
      { 
       // no file uploaded or failed upload 
       $error = array('error' => $this->upload->display_errors()); 
       $this->load->view('admin/home', $error); 
      } 
      else 
      { 
       // success 
       $data = array('upload_data' => $this->upload->data()); 
       $this->your_upload_model->add($title, $description, $data["file_name"]); 

       $this->load->view('upload_success', $data); 
      } 
     } 

    } 

這是視圖:

<?php echo validation_errors();?> 
     <?php echo form_open_multipart('login/category');?> 
     <?php 
     if(isset($error)) 
     { 
      echo $error; 
     } 
     ?> 
<table> 
<tr> 
     <td align=right>Logo:</td> 

     <td><input type="file" name="file"></td> 
     <td><input type="submit" value="submit"></td> 

     </tr> 

     </table> 

我得到兩個錯誤:

  1. 消息:未定義索引:文件
  2. 您沒有選擇要上傳的文件。

即使文本字段名稱相同,我用form_open_multipart()。此外,圖像不上傳,錯誤沒有幫助。

回答

0

首先,$_POST['file']將無法​​正常工作,但第二,你需要在​​3210提供HTML字段名稱:

if (! $this->upload->do_upload('file')) 
{ 
... 
} 

此外,刪除$image_data=$_POST['file'];因爲它無論如何都不會工作。您已經在使用$this->upload->data()這是獲取上傳的文件數據的正確方法。

原因$this->upload->do_upload()不工作沒有字段名稱是因爲它查找字段name="userfile"如果參數未提供。您正在使用name="file"

+0

我已經做了什麼u必須完成的改變,錯誤已經走了,但圖像沒有上傳和錯誤不會顯示。您可以幫助... –

+0

我已經在控制器文件夾中創建了一個名爲uploads的文件夾名稱,但圖像未上傳 –

+0

請確保'$ config ['upload_path']'已滿路徑...你可以使用'$ config ['upload_path'] = FCPATH。 '/上傳/';'。 'FCPATH'是一個CI常量來獲得前端控制器的路徑(index.php)。 – Brendan

0

使用此代碼的第一個問題:

public function category() 
{ 
if (array_key_exists('submit', $_POST)) { 
    ---------You can enter the remaining code here............ 
} 
} 

而對於第二個:

if (! $this->upload->do_upload("file")) 
{ 
-----------code here---------- 
}