我正在嘗試構建一個檔案上傳器來使用 Codeigniter 3.1.13 上傳 JSON 檔案。我使用 CI 檔案構建了基本表單,但我不斷收到錯誤訊息“不允許您嘗試上傳的檔案型別”。
我對檔案進行了 var_dump,并檢查了 mimes.php 中確實列出了該型別,所以我不確定我錯過了什么!
這是我的代碼:
public function upload_data_file(){
$this->load->helper(array('form', 'url'));
$config['upload_path'] =$this->config->item('temp_path') . "fhmFieldData";
$config['allowed_types'] = 'json';
$this->load->library('upload', $config);
var_dump($_FILES);
if ( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->render($error,'fhm/enter_data');
}
else {
$data = array('upload_data' => $this->upload->data());
$this->render($data,'fhm/enter_data');
}
}
這是 var_dump 列印的內容:
'userfile' =>
array (size=5)
'name' => string 'FHMFieldData_v2022-1_VMC593.json' (length=32)
'type' => string 'application/json' (length=16)
'tmp_name' => string '/users/v/m/vmc/phptemp/upload/php846QNI' (length=39)
'error' => int 0
'size' => int 17572
我的 mimes.php 檔案在第 117 行有這個:
'json' => array('application/json', 'text/json'),
uj5u.com熱心網友回復:
讓我的評論成為答案:
檢查您服務器的 MIME 型別,因為并非所有托管服務提供商都安裝所有 MIME 型別!
添加
text/plain
到 application\config\mimes.php 中的“json”:'json' => array('application/json', 'text/json', 'text/plain'),
注意:添加text/plain
通常也可以解決 *.csv mime 型別問題,因此對于其他應用程式請記住這一點
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/470291.html
上一篇:未定義命名引數“userId”