2016-01-02 38 views
1

如何匹配CI 3.0中的某個輸入類型擴展?Codeigniter輸入匹配某個擴展

我有以下輸入:

<input type="text" name="name" value="some.dll" id="name" maxlength="24" class="form-control" placeholder="Example: some.dll"> 

我需要制定規則,只允許輸入.dll文件,但我不`噸知道怎麼辦。 任何幫助是apreciated,非常感謝!

+0

使用[文件上傳](https://ellislab.com/codeigniter/user-guide/libraries /file_uploading.html)(免責聲明:codeigniter.com目前不在此處)類,您應該配置允許的文件類型。 – Tpojka

+0

@Tpojka - 我不上傳,我接受這個名稱,並存儲到數據庫中... –

回答

1

嘗試這種方式(在表單字段名稱注意改變我犯了太多):

class Form extends CI_Controller 
{ 

    public function index() 
    { 
     $this->load->helper(array('form', 'url')); 

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

     $this->form_validation->set_rules('dll', 'DLL file name', 'callback_dll_check');//change name and id in form view to dll and remove value attribute 

     if ($this->form_validation->run() == FALSE) 
     { 
      $this->load->view('myform'); 
     } 
     else 
     { 
      //code for insert into DB 
      //redirect('somewhere', 'refresh'); 
     } 
    } 

    public function dll_check($str) 
    { 
     $str = explode('.', $str);// 
     if (end($str) !== 'dll') 
     { 
      $this->form_validation->set_message('dll', 'The %s field has to be of "dll" extension type.'); 
      return FALSE; 
     } 
     else 
     { 
      return TRUE; 
     } 
    } 

} 
+0

好的,但我得到:無法訪問與您的字段名稱相對應的錯誤消息。和我以前的rulles哪裏這樣的函數:$ this-> form_validation-> set_rules(陣列( \t \t \t \t陣列( '場'=> 'cheat_name', \t \t \t \t '標籤'=> '郎鹹平:dll_based_cheat_name' , \t \t \t \t '規則'=> '修剪|所需| alpha_dash | MIN_LENGTH [2] | MAX_LENGTH [24]'),)) –

+0

我'認爲我真的不捕獲正確 –

+0

完成錯誤消息!謝謝你們。 –

0

要上傳的笨您使用

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

您可以指定在$ config中的「DLL」 [「allowed_types」]在初始化時的文件。

$config['allowed_types'] = 'dll'; 
$this->load->library('upload', $config); 
+0

但我沒有上傳文件,我正在存儲在數據庫中...在那種情況下我該怎麼辦? –