2012-03-29 80 views
1

我需要根據需要將輸入文件設置到我的Codeigniter控制器中。 這是我form_validation:Codeigniter - 表單驗證不適用於文件

$this->form_validation->set_rules('copertina','Foto principale','required|xss_clean'); 

,這是形式:

<?php echo form_open_multipart('admin/canile/nuovo'); ?> 
<li class="even"> 
    <label for="copertina">Foto principale <span>*</span></label> 
    <div class="input"><input type="file" name="copertina" value="<?php echo set_value('copertina'); ?>" id="copertina" /></div>  
</li> 
<?php echo form_close(); ?> 

但經過提交表單說,文件沒有設置,所以要求clausole失敗......怎麼能我修復它?

+0

它是空的,我試圖打印$ _ POST和數組中我無法找到「copertina」場......但II打印剛$ _FILES ['copertina'] ['name']我可以看到img名稱 – 2012-03-29 14:59:06

+0

'set_value()'從'$ _POST'設置值,而不是從'$ _FILES'設置。也是表單驗證是$ _POST'$ _FILES'字段沒有被該庫驗證 – Broncha 2012-03-29 15:01:54

+0

哦,我看到了......你知道一個文件驗證庫嗎? – 2012-03-29 15:03:59

回答

4

文件上傳數據未存儲在$_POST數組中,因此無法使用CodeIgniter的form_validation庫進行驗證。使用$_FILES陣列可以使用PHP上傳文件。

在運行表單驗證之前,可能使用$ _FILES數組中的數據直接操作$ _POST數組,但我沒有測試過這個。最好只檢查上傳庫進程是否有錯誤。

此外,出於安全原因,不可能(重新)設置頁面重新加載的值。

+0

你知道一個圖書館誰可以驗證文件輸入? – 2012-03-29 15:06:26

-1

你有沒有看這個 - >

http://codeigniter.com/user_guide/libraries/file_uploading.html

<?php 

class Upload extends CI_Controller { 

    function __construct() 
    { 
     parent::__construct(); 
     $this->load->helper(array('form', 'url')); 
    } 

    function index() 
    { 
     $this->load->view('upload_form', array('error' => ' ')); 
    } 

    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()) 
     { 
      $error = array('error' => $this->upload->display_errors()); 

      $this->load->view('upload_form', $error); 
     } 
     else 
     { 
      $data = array('upload_data' => $this->upload->data()); 

      $this->load->view('upload_success', $data); 
     } 
    } 
} 
?> 

更新爲根據註釋:

您可以檢查使用純PHP,如果你喜歡...

$errors_file = array(
     0=>'Success!', 
     1=>'The uploaded file exceeds the upload_max_filesize directive in php.ini', 
     2=>'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', 
     3=>'The uploaded file was only partially uploaded', 
     4=>'No file was uploaded', 
     6=>'Missing a temporary folder', 
     7=>'Cannot write file to disk' 
    ); 

if($_FILES['form_input_file_name']['error'] == 4) { 
echo 'No file uploaded'; 
} 
if($_FILES['form_input_file_name']['error'] == 0) { 
echo 'File uploaded... no errors'; 
} 
+0

謝謝,我已經看到它...但我需要檢查輸入文件是否設置,而不是空 – 2012-03-29 16:11:03

+0

看看更新的代碼有幫助嗎? – TigerTiger 2012-03-29 16:14:19

+0

我認爲即使沒有完全融入圖書館,這也是更好的解決方案! – 2012-03-30 07:26:33

3

要進行驗證爲文件工作,你必須檢查它是否爲空。

一樣,

if (empty($_FILES['photo']['name'])) 
     { 
     $this->form_validation->set_rules('userfile', 'Document', 'required'); 
     } 
0

您可以通過覆蓋CI_Form_Validation

的運行功能

複製此功能在延伸CI_Form_Validation類解決這個問題。

該函數將覆蓋父類函數。在這裏,我只加一個額外的檢查,它可以處理文件還

/** 
* Run the Validator 
* 
* This function does all the work. 
* 
* @access public 
* @return bool 
*/ 
function run($group = '') { 
    // Do we even have any data to process? Mm? 
    if (count($_POST) == 0) { 
     return FALSE; 
    } 

    // Does the _field_data array containing the validation rules exist? 
    // If not, we look to see if they were assigned via a config file 
    if (count($this->_field_data) == 0) { 
     // No validation rules? We're done... 
     if (count($this->_config_rules) == 0) { 
      return FALSE; 
     } 

     // Is there a validation rule for the particular URI being accessed? 
     $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group; 

     if ($uri != '' AND isset($this->_config_rules[$uri])) { 
      $this->set_rules($this->_config_rules[$uri]); 
     } else { 
      $this->set_rules($this->_config_rules); 
     } 

     // We're we able to set the rules correctly? 
     if (count($this->_field_data) == 0) { 
      log_message('debug', "Unable to find validation rules"); 
      return FALSE; 
     } 
    } 

    // Load the language file containing error messages 
    $this->CI->lang->load('form_validation'); 

    // Cycle through the rules for each field, match the 
    // corresponding $_POST or $_FILES item and test for errors 
    foreach ($this->_field_data as $field => $row) { 
     // Fetch the data from the corresponding $_POST or $_FILES array and cache it in the _field_data array. 
     // Depending on whether the field name is an array or a string will determine where we get it from. 

     if ($row['is_array'] == TRUE) { 

      if (isset($_FILES[$field])) { 
       $this->_field_data[$field]['postdata'] = $this->_reduce_array($_FILES, $row['keys']); 
      } else { 
       $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']); 
      } 
     } else { 
      if (isset($_POST[$field]) AND $_POST[$field] != "") { 
       $this->_field_data[$field]['postdata'] = $_POST[$field]; 
      } else if (isset($_FILES[$field]) AND $_FILES[$field] != "") { 
       $this->_field_data[$field]['postdata'] = $_FILES[$field]; 
      } 
     } 

     $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']); 
    } 

    // Did we end up with any errors? 
    $total_errors = count($this->_error_array); 

    if ($total_errors > 0) { 
     $this->_safe_form_data = TRUE; 
    } 

    // Now we need to re-set the POST data with the new, processed data 
    $this->_reset_post_array(); 

    // No errors, validation passes! 
    if ($total_errors == 0) { 
     return TRUE; 
    } 

    // Validation fails 
    return FALSE; 
}