Below is the code for file upload in CodeIgniter:
To Configure file upload, limit by size, types, and upload path.
//Upload Photo $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '5000'; //Rename the uploaded image $config['file_name'] = <NEW IMAGE NAME>;
With this, you can call upload library
$this->load->library('upload', $config);
change the photo to any variable given in the HTML Form.
if ( ! $this->upload->do_upload('photo')){
$result['error'] = $this->upload->display_errors('','');
}else{
$image = $this->upload->data();
}
If you want to resize Image
$config['image_library'] = 'gd2'; $config['source_image'] = $imagepath; $config['create_thumb'] = TRUE; $config['maintain_ratio'] = TRUE; $config['width'] = 320; $config['height'] = 480;
With this, you can call image library
$this->load->library('image_lib', $config);
and let the image library work for you:
if ( ! $this->image_lib->resize())
{
echo$this->image_lib->display_errors('','')
}else{
$img = $this->image_lib;
echo $img->full_dst_path;
}
