2016-08-15 117 views
0

我正在爲我的項目使用codeigniter,我需要讀取文件的內容。所以,我需要驗證來檢查文件是否被選中。Codeigniter表單驗證文件

這是我在控制器代碼

$this->form_validation->set_rules(
    'estimation_file', 
    'Project Estimation File', 
    'required' 
); 

但在選擇一個文件時,它會顯示錯誤說法 - 項目 估計文件字段需要

回答

2

在笨,你可以不檢查驗證二維數組或文件字段與form_validation,而不是你可以檢查後發佈的數據。

$this->form_validation->set_rules('validation_check','any required field','required'); 

if($this->form_validation->run()==FALSE) 
{ 
    // your code before posting... 
} 
else 
{ 
    // check the file posting 
    if($_FILES['estimation_file']['name']!='') 
    { 
     // if file selected or not empty 
    } 
    else 
    { 
     // if file not selected | empty | redirect 
    } 
} 

不要忘記寫ENCTYPE =「多部分/格式數據」表單字段內,否則你的文件字段將不通過二維數組的值。

<form method="post" enctype="multipart/form-data" name="upload_form" action=""> 
    <input type="hidden" name="validation_check" value="TRUE" /> 
    <input type="file" name="estimation_file" value="" /> 
    <input type="submit" value="Post" /> 
</form>