RonnyKnoxville
RonnyKnoxville

Reputation: 6394

CodeIgniter form validation does not recognise $_FILES

Im using a custom form validator function to check if an image type file is being uploaded. Its quite simple, but no matter what I try I cant seem to get the validator to pass the variable correctly.

HTML

<input type="file" name="image" /><div><?php echo form_error('image'); ?></div>

CONTROLLER

public function postAd(){
    $this->form_validation->set_rules("image", "image", "callback_valid_image");

     if ($this->form_validation->run()==FALSE){
            $this->load->view('header', array('title'=>"Post an ad"));
            $this->load->view('ads/default');
            $this->load->view('footer');
        }else{
            echo "EMPTY";
        }
}

public function valid_image($file){
     echo "RAW:" . $file . "<br />";
     echo "FILE:",$_FILES[$file];
}

I have set up the messages in the form_validation language file too.

Regardless of what type of file I enter, or if I leave it blank, it will never show any file at echo "RAW" or echo "FILE" lines.

Upvotes: 0

Views: 470

Answers (2)

W Kristianto
W Kristianto

Reputation: 9303

To set your own custom message use the following function :

$this->form_validation->set_message('rule', 'Message');

Upvotes: 0

Jordan Arsenault
Jordan Arsenault

Reputation: 7388

The Form Validation Library does not apply to the $_FILES array, only to $_POST. You should use the File Uploading Library and use something like:

if (!$this->upload->do_upload()){
    $error = array('error' => $this->upload->display_errors());
    $this->load->view('upload_form', $error);
}
else{
    //upload worked.
}

Upvotes: 1

Related Questions