Skip to content

Instantly share code, notes, and snippets.

@ronykader
Last active February 14, 2017 12:46
Show Gist options
  • Save ronykader/f53ea0a2452d78a96b38ac9d018ac817 to your computer and use it in GitHub Desktop.
Save ronykader/f53ea0a2452d78a96b38ac9d018ac817 to your computer and use it in GitHub Desktop.
Controller
---------------------------
if ( !empty($_FILES['gallery_images']['name'] ))
{
$gallery_images = $this->Gallery_model->uploadImage('gallery_images','gallery_images','gif|jpg|png',1);
$attachment = $gallery_images['file_name'];
if ( $gallery_images['file_name'] == '' )
{
$this->session->set_flashdata('FlsMsg',$this->alert->danger($gallery_images['upload_error']));
redirect( 'Gallery/gallery_form' );
}
}
else
{
$attachment = '';
}
Model
-------------------------------
public function uploadImage($field_name,$path,$type=null,$thumb=null,$width=null,$height=null,$size=null)
{
$path = './assets/images/'.$path.'/';
makedir($path);
$config['upload_path'] = $path;
$config['allowed_types'] = $type;
$config['file_ext_tolower'] = true;
$config['max_size'] = $size;
$config['max_width'] = $width;
$config['max_height'] = $height;
$this->load->library('upload', $config);
if (!$this->upload->do_upload($field_name)){
$data['upload_error'] = $this->upload->display_errors();
$data['file_name'] = '';
return $data;
}
else{
$data = $this->upload->data();
if($thumb)
{
$thumbpath = './assets/images/post_thumb/';
makedir($thumbpath);
$resize['image_library'] = 'gd2';
$resize['source_image'] = $path.$data['file_name'];
$resize['create_thumb'] = FALSE;
$resize['new_image'] = $thumbpath.'/'.$data['file_name'];
$resize['maintain_ratio'] = true;
$resize['width'] = 255;
$resize['height'] = 255;
$this->load->library('image_lib', $resize);
if ( ! $this->image_lib->resize()){
echo $this->image_lib->display_errors();
}
}
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment