Welcome Guest, Not a member yet? Register   Sign In
How to upload image file
#1

[eluser]luffy[/eluser]
as the topic title.

How to upload the image file?
#2

[eluser]frrose[/eluser]
read about it here
http://ellislab.com/codeigniter/user-gui...ading.html

Smile
#3

[eluser]luffy[/eluser]
$this->form_validation->set_rules

How to use the rule to validate the "upload file".
#4

[eluser]sherpa[/eluser]
use callback function in the rules

Code:
$this->form_validation->set_rules('fashion_thumbnail_id',"Thumbnail Image",'callback__thumbnail_check');


    function _thumbnail_check($str){
        if($_FILES['fashion_thumbnail_id']['error'] == 4){
            $this->form_validation->set_message('_thumbnail_check','The %s image should be provided.');
            return FALSE;
        }
        else{
                return TRUE;
        }
    }
#5

[eluser]luffy[/eluser]
what's callback function?

a little difficult
#6

[eluser]Shahgeb[/eluser]
simply at validation rule call the call back function
like

$rule['image_field_name'] = callback_verfiy_image();


and verfiy_image function sould be in controller
verfiy_image()
{
if(empty($_FILES['image_field_name']['name']['0'])){
$this->validation->set_message('verfiy_image', "You must have to specifiy image");
return false;
}

}

or any custom message which you want to show in set_message function


hopes it will work
#7

[eluser]luffy[/eluser]
This is my code below:

public function create_pic()
{


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

$config['upload_path'] = './';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';

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

//$this->form_validation->set_rules('userpic',"lang:form_pic",'callback_create_pic');


if ($this->form_validation->run() == TRUE)
{
//$this->posts->create_pic();
if ( ! $this->upload->do_upload('userpic')){

$this->validation->set_message('create_pic',$this->upload->display_errors());
return false;
} else {
return true;
}
$this->session->set_flashdata('message', lang('successfully_created'));

redirect('admin/posts/create_pic', 'refresh');
}

$data['categories'] = $this->posts->get_categories();
$this->_template['page'] = 'posts/create_pic';

$this->system_library->load($this->_template['page'], $data, TRUE);
}

It will return one empty page. But how to show the error message when empty file?
#8

[eluser]sherpa[/eluser]
Try the following update code
Code:
public function create_pic(){
      $this->load->library('upload', $config);

      $config['upload_path'] = './';
      $config['allowed_types'] = 'gif|jpg|png';
      $config['max_size']  = '100';
      $config['max_width']  = '1024';
      $config['max_height']  = '768';
    
      $this->upload->initialize($config);

      $this->form_validation->set_rules('userpic','lang:form_pic','callback__create_pic');
    
      if ($this->form_validation->run() == FALSE){
              redirect('admin/posts/create_pic', 'refresh');
      }
      else{
            if (!$this->upload->do_upload('userpic')){
                $this->validation->set_message('create_pic',$this->upload->display_errors());                
           }
        $this->session->set_flashdata('message', lang('successfully_created'));
      }
      
      $data['categories']      = $this->posts->get_categories();
      $this->_template['page']  = 'posts/create_pic';
      
      $this->system_library->load($this->_template['page'], $data, TRUE);
  }



/* Callback function defined here */
  function _create_pic(){
        if($_FILES['userpic']['error'] == 4){
            $this->form_validation->set_message('_thumbnail_check','The %s image should be provided.');
                return FALSE;
            }
        else{
            return TRUE;
        }
    }
#9

[eluser]luffy[/eluser]
the problem:
Unable to access an error message corresponding to your field name.
#10

[eluser]luffy[/eluser]
Another question:
How to rename the upload images?




Theme © iAndrew 2016 - Forum software by © MyBB