简体   繁体   中英

Codeigniter form validation for file

I'm using codeigniter for my project and I need to read contents of files. So,I need validation to check whether the file is selected or not.

Here is my code in controller

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

But while choosing a file it shows error saying - The Project Estimation File field is required

In codeigniter you cannot check the validation of two dimension array or file field with form_validation, instead you can check it after posting the data.

$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
   }
}

do not forget to write enctype="multipart/form-data" within the form field, otherwise your file field will not pass the value of two dimension array.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM