How you can upload an image using helper function in codeigniter?

Saif Shakil Codigniter 25 January, 2018 : 11:21 AM

How you can upload an image using helper function in codeigniter?

In this tutorial, I will try to explain how you can upload images by a helper function. Now you can ask why helper function ,right? Let me explain. 

In a project, i'm pretty sure you will need image function on several places. So you might use same code again and again with different storage path, size, dimension settings. This is where helper function becomes handy! 

Why Helper Function?

PHP helper functions are designed to save your time and repetitive task. It will speed up your project progress. 

How to use it?

In here, first we will create a helper function. to create a helper function  go to - application/helpers folder. then create a php file with your desired name. For example in my case it  is - 'file_upload_helper.php'.

Now Lets create a function - image_upload on that file_upload_helper.php file. 

if (!function_exists('image_upload')) {
function image_upload($url,$image_path)
{

$ci =& get_instance();

$config['upload_path'] = $image_path;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 1024;
$config['max_width'] = 1600;
$config['max_height'] = 1600;
$config['maintain_ratio'] = TRUE;
$config['remove_spaces'] = TRUE;

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

if (!$ci->upload->do_upload('image_upload')) {
$error = array('error' => $ci->upload->display_errors());

$ci->session->set_flashdata('error', $error['error']);
redirect($url, 'refresh');

} else {
$image = $ci->upload->data();
return $config['upload_path'] . $image['file_name'];
}

return FALSE;

}

}
First of all we are checking if there is any function with same name by if (!function_exists('image_upload'))

Now in parameter here in function's parameter we are receiving $url for redirecting purpose. If image upload failed error message will show on that url page. $image_path is for storage page.

Here all the configurations are basic config of an image upload in codeigniter. You can find out more about this config from here. After uploading the image it will return upload path and image name like this './image_upload/image_name.jpg'. 

$this->load->library('image_lib');
$this->load->library('user_agent');
/*
* STORING IMAGE INFORMATION
* STORING IMAGE & RETURNS IMAGE PATH BY HELPER FUNCTION
* AGENT LIBRARY RETURNS PREVIOUS/REFERRAL URL
*/

public function store_image()
{
$data = array();
$data['caption'] = $this->input->post('caption');
$data['image_type'] = $this->input->post('image_type');
$data['visibility_status'] = $this->input->post('visibility_status');

$this->load->helper('file_upload_helper');

if ($this->agent->is_referral()) {
$refer = $this->agent->referrer();
}
$upload_path = './image_upload/';
$data['image'] = image_upload($refer,$upload_path);

$this->db->insert('image_gallery', $data);

$this->session->set_flashdata('success', 'Image Uploaded and Information Saved!');

redirect($refer, 'refresh');
}


First of all Load Load Image Library and User Agent Library helper in autoload array.

Note: To add helpers in autoload go to application/config/autoload.php, and add file name in helpers array like this: 

$autoload['helpers'] = array('file_upload_helper');

Or you can load helpers manually. Image Library for uploading image with config settings. and user agent to get previous/referral url. Then, load the helper file you have created like this. 

$this->load->helper('file_upload_helper');

then get the url from agent and set storage path of image like this - 

if ($this->agent->is_referral()) {
$refer = $this->agent->referrer();
}
$upload_path = './image_upload/';

then just pass the $url and $upload path to the helper where function is expecting parameter which i have already explained above. 

$data['image'] = image_upload($refer,$upload_path);

after completing execution of image upload, the image path will be returned. now you can save it to your database.


TAGS: PHP Codeigniter Image Upload Codeigniter Helper Function Codeigniter
Views: 666

Related Posts you may like