2015-11-07 170 views
1

我正在使用codeigniter 3,在​​3210函數中得到如You did not select a file to upload.的錯誤,但正常的php函數move_uploaded_file()正常工作。我提到了大多數來自stackoverflow的答案,但我沒有得到解決方案。Codeigniter文件上傳錯誤:'您沒有選擇要上傳的文件'

我認爲這可能在wamp問題,但我沒有弄清楚它在哪裏。 如果這段代碼在你的機器上工作,那麼它將在我的wamp php或Apache設置中出現。

查看:(upload.php的)

<!DOCTYPE HTML> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
</head> 
<body> 
<?php echo form_open_multipart('welcome/do_upload'); ?> 
    <input id="sfile" type="file" name="file"> 
    <input type="submit" value="Submit"> 
<?php echo form_close(); ?> 
</body> 
</html> 

控制器:(的welcome.php)

function do_upload() 
    { 
     $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()) //not working 
     { 
      $error = array('error' => $this->upload->display_errors()); 
      var_dump($error); 
     } 
     else 
     { 
      $data = array('upload_data' => $this->upload->data()); 
     } 
     echo $basename = $_FILES['file']['name']; 
     move_uploaded_file($_FILES['file']['tmp_name'],$config['upload_path'].$basename); 
     // move_uploaded_file() works fine 
    } 
+0

嘗試在'$這個 - > upload-> do_upload(「文件」)',並檢查 – Saty

+0

@Saty是的,我嘗試過同樣的問題 – 151291

+1

檢查文件的大小你,因爲你設置上傳'$通過字段名config ['max_size'] ='100';'。這意味着你不能喲上傳文件大小超過100 kb – Saty

回答

1

按照CI 3,你需要通過名稱屬性,在$this->upload->do_upload('')

所以你需要通過name屬性在文件中它

$this->upload->do_upload('file'); 

檢查大小,你已經上傳,因爲你設置$ config['max_size'] = '100';。這意味着你將無法上傳文件的大小爲100多個KB

File Uploading Class

0

先裝上載庫,然後初始化後的配置數據。

並在$ this-> upload-> do_upload('file')中傳遞輸入文件類型的屬性名稱。

function do_upload() 
{ 
    $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); # Load upload library 
    $this->upload->initialize($config); # Initialize 
    if (! $this->upload->do_upload('file')) 
    { 
     $error = array('error' => $this->upload->display_errors()); 
     var_dump($error); 
    } 
    else 
    { 
     $data = array('upload_data' => $this->upload->data()); 
    } 

} 
相關問題