/*
* Controller variables
*/
var $fields_image = array(
'image' => ''
);
var $params_imgs = array(
'upload_path' => './galery/',
'allowed_types' => 'jpg|png|jpeg|gif',
'file_name' => '',
'overwrite' => TRUE
);
/*
* Before $this->form_validation->run();
*/
$this->form_validation->set_rules('image', 'Image', 'trim|required|xss_clean|callback_validImg[image]');
/*
* Function in the same controller
*/
function validImg( $str, $name ){
if( $_FILES[$name]['error']==0 && $_FILES[$name]['size']>0 ){
$this->load->library('upload');
$this->params_imgs['file_name']=$name.'_'.time();
$this->upload->initialize($this->params_imgs);
if ( ! $this->upload->do_upload($name) ){
$error = array('error' => $this->upload->display_errors());
foreach($error as $txt )
$this->form_validation->set_message('validImg', '<b>%s</b> '.$txt);
return false;
}else{
$file_info = $this->upload->data();
$this->fields[$name] = $file_info['file_name'];
}
}
return true;
}
Upload a file in the controller with the form validation.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.