2016-12-06 69 views
1

這已被問了很多,但嘗試了很多事情後,我仍然無法找到解決此問題的原因。「上傳路徑似乎不是有效的」Codeigniter圖像上傳

我自動加載上傳庫。

我的文件夾結構是:

/application/ 
/assets/ 
    css/ 
    images/ 
     hero/ 
    js/ 
/system/ 

我的控制器:

$fileName = $this->input->post('title') . '_hero'; 
$filePath = './assets/images/hero/'; 
$config['allowed_types'] = 'gif|jpg|png'; 
$config['overwrite'] = true; 

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

if (!empty($_FILES['heroimage'])) { 
    $config['file_name'] = $fileName; 
    $config['upload_path'] = $filePath; 

    if (!$this->upload->do_upload('heroimage')) { 
     $error = array('error' => $this->upload->display_errors()); 
     var_dump($error); 
    } else { 
     $data = array('upload_data' => $this->upload->data()); 
     var_dump($data); 
    } 
} 

我曾嘗試都初始化現在使用$this->load->library('upload', $config);怎麼但是沒有工作過。

我也檢查目錄是否存在,如果它是可寫的,並且都返回true。

我試過被上傳路徑設置爲$config['upload_path'] = FCPATH.'assets\images\hero';

任何指着我的方向是正確的幫助的另一件事,將不勝感激。

回答

3

你應該有配置數據一樣,因爲文件路徑未初始化

https://www.codeigniter.com/user_guide/libraries/file_uploading.html#setting-preferences

$fileName = $this->input->post('title') . '_hero'; 
$filePath = './assets/images/hero/'; 
$config['upload_path'] = $filePath; 
$config['allowed_types'] = 'gif|jpg|png'; 
$config['overwrite'] = true; 

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

if (!empty($_FILES['heroimage'])) { 

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

     // errors 

    } else { 

     $upload_data = $this->upload->data(); 

     echo $upload_data['file_name']; 

    } 
} 
+0

*捂臉*這是問題。這是昨晚深夜! –