2013-04-18 58 views
0

我有一個頁面的表單。表單上有一個下拉列表和一些輸入框和一個文件上傳。如果我嘗試提交我的頁面,則會出現一個空白頁面,並顯示錯誤「不存在的類:上傳」。我還沒有使用文件上傳。codeigniter文件上傳與其他元素

這是我的輸入文件類型:

<tr><td>Image: </td></tr> 
<tr><td><input type="file" name="image" size="20" /></td></tr> 

我的,你需要一個form_open_multipart(「上傳」)的CI文件上傳類閱讀;那麼,我只是有這種形式,還是必須在我的情況下做其他事情?因爲我在該表單上有其他輸入類型?

這裏是我的控制器的一點點代碼:

$subject = htmlspecialchars(trim($this->input->post('subject'))); 
$message = htmlspecialchars(trim($this->input->post('message'))); 
$image = $this->input->post('image'); 

$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); 

if (!$this->upload->do_upload($image)) {   
    $data['fout'] = $this->upload->display_errors(); 
    $this->annuleren($soort, $data); 
} else { 
    $data['upload'] = $this->upload->data(); 
} 

$id = $this->message_model->insert($message, $subject, $image); 

redirect('infoscreen/dashboard', 'refresh'); 

我也做了一個文件夾「上傳」下「源文件」。我可能在做一些愚蠢的事情。有人可以幫助我,並告訴我我做錯了什麼,我該如何解決它?

非常感謝:)

回答

2

編輯:首先加載上傳庫:

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

的另一個問題在於,如果引用文件錯誤的事實。

此行unneccesary:

$image = $this->input->post('image'); // <- remove this line 

,而不是僅僅使用:

if (!$this->upload->do_upload('image')) { 

笨,缺省情況下尋找一個名爲「userfile的」文件輸入,但如果這是不存在,您需要添加'image'到這樣的​​3210方法。

那麼你也行:

$id = $this->message_model->insert($message, $subject, $image); 

我不能確定你的想法應該在$image可變的,但如果上傳succeded你可以從$data['upload']附加數據:

$id = $this->message_model->insert($message, $subject, $data['upload'][file_name]); // <- gets the filename 

全文請參考:http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

同樣回答你的第二個問題,它不是一個問題,multipart表單類型有其他輸入字段/類型。

+0

噢好吧,但我仍然有這個錯誤。你把userfile添加到do_upload意味着什麼? – Lewis

+0

啊,是的,你需要'$ this-> load-> library('upload');'看看我的編輯。 – jtheman

+0

即使我註釋掉 - > do_upload代碼等,它仍然會給出相同的錯誤。 – Lewis