Back to Top

How to upload image in CodeIgniter?

Image Uplod in CodeIgniter

CodeIgniter makes it easy to add images to your site. You can upload them using code I am going to explain here in this post that How to upload image in CodeIgniter?.If you want to upload an image in CodeIgniter, you need to write following HTML code into your view file which will create the field to upload an image.

Select File

Now in the form action, at the PHP side, your code should be this. Here txtImage is your input field name. You have to provide upload_path, allowed types, max_size etc. You can ecrypt the file name by passing encrypt_name = true. The else part creates image thumbnail for specified height and width.

if($_FILES['txtImage']['name'] != "")
	$config['upload_path'] = 'images/upload/';
	$config['allowed_types'] = 'gif|jpg|png|jpeg';
	$config['max_size'] = '2000';
	$config['remove_spaces'] = true;
	$config['overwrite'] = false;
	$config['encrypt_name'] = true;
	$config['max_width']  = '';
	$config['max_height']  = '';
	$this->load->library('upload', $config);
	if (!$this->upload->do_upload('txtImage'))
		$error = array('error' => $this->upload->display_errors());
		$image = $this->upload->data();
		if ($image['file_name'])
			$data['file1'] = $image['file_name'];
		$product_image = $data['file1'];
		$config['image_library'] = 'gd2';
		$config['source_image'] = '/images/upload/'.$data['file1'];
		$config['new_image'] = '/images/upload/new/';
		$config['maintain_ratio'] = FALSE;
		$config['overwrite'] = false;
		$config['width'] = 280;
		$config['height'] = 280;
		$this->load->library('image_lib', $config); //load library
		$this->image_lib->resize(); //do whatever specified in config
		echo "all the things go well.";

That’s it, Your image is get uploaded to the can have a look into the folder. 🙂

Thank you for reading, good luck and let me know if you run into any problems & I’ll help as much as I can 🙂

Hope this post will helpful for you, waiting for your responses.Thanks for reading and feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Comments (4)

  1. Thanks For Your Tutorial. I Have Success In My Coding to Upload Image File. Yeah. Thanks….

  2. akhtar ansari says:

    very nice

  3. i upload my file in remote server …but my file not upload in remote side…but the file store in local host … what is the problem…plz give me the solution sir….

  4. nice dude your the man . thanks a lot .

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to change Quotes in WordPress

Posted on 6 years ago


PHP 5 Abstract classes and Methods

Posted on 7 years ago


What Is Making People Insane for Cake PHP?

Posted on 6 years ago


Hello world

Updated 5 days ago

Nick Carter

In depth Understanding of Laravel Core

Updated 2 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – August 2014

Posted on 4 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago