how to validate image from form validation library codeigniter

13,606

Solution 1

It will not work in form validation try it like,

function validate_image(){
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']    = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

    $this->upload->initialize($config);
    if (!$this->upload->do_upload('userfile'))
    {
        $this->validation->set_message('userfile',$this->upload->display_errors());
        return false;
    } else {
        return true;
    }
}  

Refer this

Solution 2

You can do it this way: Controller:

public function add_photo() { 
    $this->form_validation->set_rules('product_image','Product Image','callback_validate_image');
    if($this->form_validation->run()==FALSE){
        $this->add($category_id);    
    }else{
        //your logic here
    }
}

Callback Validation Method:

public function validate_image() {
    $config = array(
        'allowed_types'=>'jpg|png',
        'upload_path'=> realpath(APPPATH . '../assets/img'),
        'max_size'=>1
        );
    $this->load->library('upload', $config);
    if (!$this->upload->do_upload('product_image'))
    {
        $this->form_validation->set_message('validate_image',$this->upload->display_errors());
        return false;
    } else {
        return true;
    }
}

APPPATH will give you the path to the application folder of codeigniter.
realpath() will give you the actual path without the ../

Share:
13,606
MOHD TAHIR
Author by

MOHD TAHIR

I am a PHP Developer

Updated on June 13, 2022

Comments

  • MOHD TAHIR
    MOHD TAHIR almost 2 years

    I am trying to validate an image from my form validation's extended class in codeigniter but it is not working

    controller

    public function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
    }
    
    public function add_photo() { 
    
     $this->form_validation->set_rules('pic','Photo','validate_image['.$_FILES['pic'].']');
    
    
      if($this->form_validation->run()) {
       //some further coding
      }
    }
    

    in my library

    class MY_Form_validation extends CI_Form_validation {
    
            public function __construct() {
            parent::__construct();
            $this->_CI = &get_instance();
        }
    
    public function validate_image($image = NULL) {
            print_r($image);
            $file_name      =   $image['name'];
    
            $allowed_ext    =   array('jpg', 'jpeg', 'png', 'gif', 'bmp');
            $ext                =   strtolower(end(explode('.', $file_name)));
    
            $allowed_file_types =   array('image/jpeg','image/jpg','image/gif','image/png');
            $file_type              =   $image['type'];
    
            if(!in_array($ext, $allowed_ext) && !in_array($file_type, $allowed_file_types)) {
                $this->_CI->form_validation->set_message('validate_image', 'This file type is not allowed');
                return false;
            }
    
            if($image['size'] > 2097152) {
                $this->_CI->form_validation->set_message('validate_image', 'File size Exeeds');
                return false;
            } else {
                return true;
            }
        }
    
    
    }
    

    any suggestion and help will be appriciated thanks.