CodeIgniter - File upload required validation

I have 2 text fields and 1 file upload that are all required. Everything works when I require just the text fields, but when I require the file upload the validation error stays saying that a file is required, even though I selected one. Anyone know what I am doing wrong? Many thanks in advance.

//view

<?php echo form_open_multipart('add'); ?>

<fieldset>
    <input type="text" name="name" /><br>
    <input type="text" name="code" /><br>
    <input type="file" name="userfile" /><br><br>
    <input type="submit"value="Add" />
</fieldset>

<?php echo form_close(); ?>

//controller

public function add() {

    $this->form_validation->set_rules('name', 'Name', 'required');
    $this->form_validation->set_rules('code', 'Code', 'required');
    //$this->form_validation->set_rules('userfile', 'Document', 'required');
    //when the above line is active the upload does not go through

    if ($this->form_validation->run() == FALSE) {

        $data['page_view'] = $this->page_view;
        $data['page_title'] = $this->page_title;
        $this->load->view('template', $data);
    }
    else
    {
        $this->load->library('upload');

        if (!empty($_FILES['userfile']['name']))
        {
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';     

            $this->upload->initialize($config);

            if ($this->upload->do_upload('userfile'))
            {
                $img = $this->upload->data();
                $file_name = $img['file_name'];

                $name = $this->input->post('name');
                $code = $this->input->post('code');

                $this->load->model('create', 'create_model');
                $this->create_model->create_entry($name, $code, $file_name);

                $data['page_view'] = $this->page_view;
                $data['page_title'] = $this->page_title;
                $this->load->view('template', $data);
            }
            else
            {
                echo $this->upload->display_errors();

            }
        }
    }
}

Solution 1:

I found a solution that works exactly how I want.

I changed

$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('code', 'Code', 'trim|required');
$this->form_validation->set_rules('userfile', 'Document', 'required');

To

$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('code', 'Code', 'trim|required');
if (empty($_FILES['userfile']['name']))
{
    $this->form_validation->set_rules('userfile', 'Document', 'required');
}

Solution 2:

CodeIgniter file upload optionally ...works perfectly..... :)

---------- controller ---------

function file()
{
 $this->load->view('includes/template', $data);
}

function valid_file()
{
 $this->form_validation->set_rules('userfile', 'File', 'trim|xss_clean');

 if ($this->form_validation->run()==FALSE) 
 {
    $this->file();
 }
 else
 {
  $config['upload_path']   = './documents/';
  $config['allowed_types'] = 'gif|jpg|png|docx|doc|txt|rtf';
  $config['max_size']      = '1000';
  $config['max_width']     = '1024';
  $config['max_height']    = '768';

  $this->load->library('upload', $config);

  if ( !$this->upload->do_upload('userfile',FALSE))
  {
    $this->form_validation->set_message('checkdoc', $data['error'] = $this->upload->display_errors());

    if($_FILES['userfile']['error'] != 4)
    {
        return false;
    }

  }
  else
  {
    return true;
  }
}

i just use this lines which makes it optionally,

if($_FILES['userfile']['error'] != 4)
{
 return false;
}

$_FILES['userfile']['error'] != 4 is for file required to upload.

you can make it unnecessary by using $_FILES['userfile']['error'] != 4, then it will pass this error for file required and works great with other types of errors if any by using return false , hope it works for u ....

Solution 3:

check this form validation extension library can help you to validate files, with current form validation when you validate upload field it treat as input filed where value is empty have look on this really good extension for form validation library

MY_Formvalidation

Solution 4:

you can use call back function, like this

  $this->form_validation->set_rules('userfile', 'Document', 'callback_file_selected_test');

    if ($this->form_validation->run() == FALSE) {
         //error
     }
    else{
           // success       
      }

function file_selected_test(){

    $this->form_validation->set_message('file_selected_test', 'Please select file.');
    if (empty($_FILES['userfile']['name'])) {
            return false;
        }else{
            return true;
        }
}