Upload Image/File Into Database Ajax Codeigniter

Upload Image/File Into Database Ajax Codeigniter

Codeigniter 3 Ajax Image Upload – We will share with how to upload image using ajax into database & folder. We will upload the image using ajax and jquery. We will upload the image using jquery ajax without page refresh and reload.

In this codeigniter ajax image upload tutorial with live preview. We will upload image into database and folder. Before upload image in database and folder we will show preview of the image.

In every web development, we need to upload image into database without refreshing and reloading the whole web page. In this codeigniter tutorial you will learn simple ajax image upload step by step.

Codeigniter Ajax Image Upload


  • Download Codeigniter Latest
  • Basic Configurations
  • Create Database With Table
  • Setup Database Credentials
  • Make New Controller
  • Create Views
  • Start Development server
  • Conclusion

Download Codeigniter Project

In this step we will download the latest version of Codeigniter, Go to this link Download Codeigniter. Download the fresh setup of codeigniter and unzip the setup in your local system xampp/htdocs/ and also rename your project folder name to demo.

Basic Configurations

Next we will set the some basic configuration on config.php file, so let’s go to application/config/config.php and open this file on text editor.

Set Base URL like this

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

Create Database With Table

In this step, we need to create database name demo, so let’s open your phpmyadmin and create the database with the name demo . After successfully create a database, you can use the below sql query for creating a table in your database.

id int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(255) COLLATE utf8_unicode_ci NOT NULL,

Setup Database Credentials

$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => '',
	'database' => 'demo',
	'dbdriver' => 'mysqli',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE

Create Controller

Now we need to create a controller name Ajax.php. In this controller we will create some method/function name image() and ajaxImageStore() for showing and storing a image into database and folder.

Note:- Before upload a image go to your project root directory and create a folder name uploads where we will store a image.

class Ajax extends CI_Controller {
    public function __construct()

    public function image()

    function ajaxImageStore()  
              $config['upload_path'] = './uploads/';  
              $config['allowed_types'] = 'jpg|jpeg|png|gif';  
              $this->load->library('upload', $config);  
                  $error =  $this->upload->display_errors(); 
                  echo json_encode(array('msg' => $error, 'success' => false));
                   $data = $this->upload->data(); 
                   $insert['name'] = $data['file_name'];
                   $getId = $this->db->insert_id();

                   $arr = array('msg' => 'Image has not uploaded successfully', 'success' => false);

                    $arr = array('msg' => 'Image has been uploaded successfully', 'success' => true);
                   echo json_encode($arr);

Create Views

Now we need to create view for showing image upload form, so go to application/views/ and here create one view name image.php. After create this view put the below html in view.

<!DOCTYPE html>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<style type="text/css">
  #blah {
  width: 600px;
  height: 300px;
  border: 2px solid;
  display: block;
  margin: 10px;
  background-color: white;
  border-radius: 5px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
  transition: all 0.3s cubic-bezier(.25,.8,.25,1);
  overflow: hidden;
 <div  class="container">
   <div class="row">
   <form method="post" id="upload_form" enctype="multipart/form-data">  
     <div class="col-md-7">
       <h1> Upload & Display Image</h1></br>
        <div id="divMsg" class="alert alert-success" style="display: none">
         <span id="msg"></span>
        <img id="blah" src="//www.tutsmake.com/ajax-image-upload-with-preview-in-codeigniter/" alt="your image" /></br></br>
         <input type="file" name="image_file" multiple="true" accept="image/*" id="finput" onchange="readURL(this);"></br></br>
         <button class="btn btn-success">Submit</button>
     <div class="col-md-5"></div>

After create a image.php file and put the above html, Next we need to put the ajax image sending code to the above file so put the below code in image.php file after closing a body tag.

<script type="text/javascript">
 function readURL(input) {
      if (input.files && input.files[0]) {
          var reader = new FileReader();

          reader.onload = function (e) {
                  .attr('src', e.target.result);


      $('#upload_form').on('submit', function(e){  
           if($('#image_file').val() == '')  
                alert("Please Select the File");  
                     url:"<?php echo base_url(); ?>ajax/ajaxImageStore",   
                     data:new FormData(this),  
                     contentType: false,  
                     cache: false,  
                     dataType: "json",
                        if(res.success == true){
                        else if(res.success == false){
                        }, 3000);

Start Development server

For start development server, Go to the browser and hit below the url.



In this codeigniter ajax image tutorial – We have created a form for showing and uploading a image into database and folder.,

If you have any questions or thoughts to share, use the comment form below to reach us.


Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

3 replies to Upload Image/File Into Database Ajax Codeigniter

  1. thanks for the tutorial. It’s very helpful for me

  2. Nice post….very usefull…happy coding..

  3. thanks Sir

Leave a Reply

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