Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
CodeIgniter Upload Image File with preview using Jquery Ajax

CodeIgniter Upload Image File with preview using Jquery Ajax

Posted on September 23, 2019December 17, 2022 By XpertPhp No Comments on CodeIgniter Upload Image File with preview using Jquery Ajax

In this tutorial, We will tell you how to upload images with preview using Jquery Ajax in Codeigniter(CodeIgniter Upload Image File with preview using Jquery Ajax). upload images and preview images are the most important parts of the website.

The Codeigniter providing uploading file and resize file library. so let’s see below the following example for image upload using ajax in Codeigniter(file upload using ajax in Codeigniter).

Overview

Step 1: Download and install Codeigniter
Step 2: Create a Database in table
Step 3: Connect to Database
Step 4: Create Controller
Step 5: Create a Model
Step 6: Create The View

Step 1: Download and install Codeigniter
We are going to install Codeigniter 3, first, we will download a fresh Codeigniter 3 version and install it in our system. so you can follow below Link for Download CodeIgniter.

Step 2: Create a Database in table
In this step, we have to create a table in the database, so we will create a database using the below code.

1
2
3
4
5
CREATE TABLE product(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(164),
image VARCHAR(164)
)ENGINE=INNODB;

Step 3: Connect to Database
Go to the config folder and open database.php file some changes in this file like hostname, database username, database password, and database name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'enter here database name',
'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
);

Step 4: Create Controller
In this step, we will create a Upload_image.php file in the “application/controller” directory and paste the below code in this controller.
application/controller/Upload_image.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
 
class Upload_image extends CI_Controller {
 
 
    /**
    * Constructor of Controller
    *
    * @return Response
   */
    public function __construct() {
        parent::__construct();
        $this->load->model('upload_model');
    }
 
    function image_upload()  
{  
   $this->load->view('image_upload');  
}  
    function ajax_upload()  
    {  
   if(isset($_FILES["file"]["name"]))  
   {  
$config['upload_path'] = './assets/images';  
$config['allowed_types'] = 'jpg|jpeg|png|gif';  
$this->load->library('upload', $config);  
if(!$this->upload->do_upload('file'))  
{  
echo $this->upload->display_errors();  
}  
else  
{  
$data = array('upload_data' => $this->upload->data());
$title= $this->input->post('title');
$image= $data['upload_data']['file_name'];
$result= $this->upload_model->save_upload($title,$image);
  
echo '<img src="'.base_url().'assets/images/'.$image.'" width="300" height="225" class="img-thumbnail" />';  
}  
   }  
    }  
}
 
?>
See also  Codeigniter Multiple Files Upload Example

Step 5: Create a Model
In this step, we will create a Upload_model.php file in the “application/models” directory and paste the below code in this model.
application/models/Upload_model.php

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
class Upload_model extends CI_Model{
    function save_upload($title,$image){
        $data = array(
                'title' => $title,
                'image' => $image
            );  
        $result= $this->db->insert('product',$data);
        return $result;
    }    
}
?>

Step 6: Create The View
Finally, we will create the image_upload.php file in the views directory. here in this file use below following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>CodeIgniter Upload Image File with preview using Jquery Ajax - XpertPhp</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
    <div class="row">
<div class="col-sm-4 col-md-offset-4">
<h4>Upload files using Codeigniter and Ajax</h4>
<form class="form-horizontal" id="submit">
<div class="form-group">
<input type="text" name="title" class="form-control" placeholder="Title">
</div>
<div class="form-group">
<input type="file" name="file">
</div>
<div class="form-group">
<button class="btn btn-success" id="btn_upload" type="submit">Upload</button>
</div>
</form>  
</div>
</div>
    <div class="row">
<div class="col-sm-4 col-md-offset-4" id="uploaded_image">  
</div>
</div>
    </div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $('#submit').submit(function(e){
            e.preventDefault();
$.ajax({
url:'<?php echo base_url();?>upload_image/ajax_upload',
type:"post",
data:new FormData(this),
processData:false,
contentType:false,
cache:false,
async:false,
success: function(data){
$('#uploaded_image').html(data);
}
});
        });
    });
</script>
</body>
</html>

Codeigniter, Ajax, Jquery, MySql Tags:codeigniter with ajax, Image Upload, preview image

Post navigation

Previous Post: Laravel 6 Multiple Authentication Example Tutorial
Next Post: Codeigniter Multiple Files Upload Example

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Angular 15 CRUD Application Example Tutorial
  • Laravel 10 Form Validation Example Tutorial
  • Angular 15 Custom Form Validation Example
  • Laravel 10 Send Email Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com