2012-03-29 2 views
1

필자는 Codeigniter Controller에 입력 파일을 설정해야합니다. 이 내 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 이름을 볼 수 있습니다. –

+0

'set_value()'는'$ _POST'의 값을 설정하고'$ _FILES'의 값을 설정하지 않습니다. 또한 폼 유효성 검사는 $ _POST에 대한'$ _FILES' 필드는 해당 라이브러리에 의해 유효성이 확인되지 않습니다 – Broncha

+0

오, 알 겠어 ... 파일 유효성 검사를위한 라이브러리를 아십니까? –

답변

4

파일 업로드 데이터는 $_POST 배열에 저장되지 않으므로 CodeIgniter의 form_validation 라이브러리를 사용하여 유효성을 검사 할 수 없습니다. 파일 업로드는 $_FILES 배열을 사용하여 PHP에서 사용할 수 있습니다.

폼 유효성 검사를 실행하기 전에 $ _POST 배열의 데이터를 사용하여 $ _POST 배열을 직접 조작 할 수는 있지만 테스트하지는 않았습니다. 업로드 라이브러리 프로세스에서 오류를 확인하는 것이 가장 좋습니다.

또한 보안상의 이유로 페이지를 다시로드 할 때 값을 다시 설정하는 것은 불가능합니다.

+0

파일 입력을 확인할 수있는 라이브러리를 아십니까? –

-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

고마워, 이미 보았지만 ... 입력 파일이 설정되어 있고 비어 있지 않은지 확인해야합니다. –

+0

업데이트 된 코드가 도움이되는지 확인 하시겠습니까? – TigerTiger

+0

나는 이것이 라이브러리와 완벽하게 통합되지 않은 경우에도 더 나은 솔루션이라고 생각합니다! –

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; 
} 
관련 문제