How To Upload Image And File In CodeIgniter With Example?


In this post, we will learn how to upload images in Codeigniter. all most back-end projects for like user profile images uploads, product image upload, etc.

So in this tutorial, I am going to share with you a small example of image upload in Codeigniter. So you can simply identify how to upload image in Codeigniter, we can simply use validation for mime type, file max size, etc. In this tutorial, I will use "upload" library of Codeigniter. Codeigniter provides a pre-define upload library that way we can simply use it with image upload or file upload. In this tutorial, I also use "form" helper and "url" helper.

So, let's follow a few bellow steps to make an image uploading example.

Step 1: Download Fresh Codeigniter 3

We will download a fresh version of Codeigniter 4, so if you haven't download yet then download from here: Download Codeigniter 3.

Step 2: Add Route

In this step, you have to add some routes in your route file. So first we will create a route for image uploading example.so put the bellow content in route file:

application/config/routes.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');


$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['image-upload/post'] = "ImageUpload/Imageupload"; 
?>
Step 3: Create Controller

we have to create "ImageUpload" controller with uploadImage(). so create file in this path application/controllers/ImageUpload.php and put bellow code in this file:

application/controllers/ImageUpload.php

<?php


class ImageUpload extends CI_Controller {


   /**
    * Manage __construct
    *
    * @return Response
   */
   public function __construct() { 
      parent::__construct(); 
      $this->load->helper(array('form', 'url')); 
   }


   /**
    * Manage index
    *
    * @return Response
   */
   public function index() { 
      $this->load->view('imageUploadForm', array('error' => '' )); 
   } 


   /**
    * Manage uploadImage
    *
    * @return Response
   */
   public function uploadImage() { 


      $config['upload_path']   = './uploads/'; 
      $config['allowed_types'] = 'gif|jpg|png'; 
      $config['max_size']      = 1024;
      $this->load->library('upload', $config);


      if ( ! $this->upload->do_upload('image')) {
         $error = array('error' => $this->upload->display_errors()); 
         $this->load->view('imageUploadForm', $error); 
      }else { 
         print_r('Image Uploaded Successfully.');
         exit;
      } 
   }


} 


?>
Step 4: Create View

we will create imageUploadForm.php view file. In this file we will write design of HTML form.

application/views/imageUploadForm.php

<!DOCTYPE html>
<html> 
<head> 
  <title>Image upload in codeigniter</title> 
</head>

<body> 

  <?php echo $error;?>
<form method="post" enctype="multipart/form-data" action="<?php echo base_url();?>image-upload/post">  
     <input type="file" name="image" size="20" />
     <input type="submit" value="upload" /> 
  </form> 


</body>
</html>

Then We have to create "uploads" folder on your root directory before run example.

Now you can open bellow URL on your browser:

http://localhost:8000/image-upload

Make Sure, First you have to set base URL on your bellow configuration file :

application/config/config.php

$config['base_url'] = 'http://localhost:8000/';



I hope it can help you...

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!

close