How to Upload image in codeigniter

Home > Codeigniter Tutorial > How to Upload image in codeigniter



How to Upload file & image in codeigniter

There are 3 Steps to Upload image in Codeigniter.

Step 1 :Folder Creation

First We need to Create a folder which form the basis of the upload process.

This folder is the destination folder where image will store after Uploading .

Go to the root of the CI installation -> Create a folder named "upload". All Imags will Store inside "Upload " Folder.

Step 2 :Create Controller

Second Step is Create a controller file and save inside Controller folder. Name of the Controller is "ImageUpload_Controller.php".

In this Controller, firstl I will load two "library" and Second I will load a "Form helper" class, with the following code.

$this->load->library('form_validation');
$this->load->library(‘upload’);
$this->load->helper('url', 'form');

Add the following Code in controller (ImageUpload_Controller.php)

<?php 
class ImageUpload_Controller extends CI_Controller
{
	function __construct()
	{
	parent::__construct();
	//load Helper for Form
	$this->load->helper('url', 'form');	
	$this->load->library('form_validation');
	}
	function index()
	{
		$this->load->view('imageupload_form');
	}
	public function upload() 
	{
        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = 2000;
        $config['max_width'] = 1500;
        $config['max_height'] = 1500;

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

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

            $this->load->view('imageupload_form', $error);
        } 
		else 
		{
            $data = array('image_metadata' => $this->upload->data());

            $this->load->view('imageupload_success', $data);
        }
    }

}
?>

Step 3 :View

For the Structure of image upload, I have created two files that is stored inside Ci root view folder.

First file is to upload image and Second file is display Success message.

Structure of "imageupload_form.php" this for uploading a image.

<!DOCTYPE html>
<html>
<head>
    <title>Upload Image in Codeigniter</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <?php echo @$error; ?> 
	<?php echo form_open_multipart('ImageUpload_Controller/upload');?>
	<?php echo "<input type='file' name='profile_pic' size='20' />"; ?>
	<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
	<?php echo "</form>"?>
</body>
</body>
</html>

Success Message View

<!DOCTYPE html>
<html>
<head>
    <title>Image Uploaded Success</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
   <h1>Congreates image uploaded successfully</h1>
</body>
</html>
Output

After Uploading Check image

Total Downloads : 2

Login / Register To Download