CodeIgniter Upload Image File with preview using Jquery Ajax – onlinecode

CodeIgniter Upload Image File with preview using Jquery Ajax – onlinecode

In this post we will give you information about CodeIgniter Upload Image File with preview using Jquery Ajax – onlinecode. Hear we will give you detail about CodeIgniter Upload Image File with preview using Jquery Ajax – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this tutorial, We will tell you how to upload images with preview using Jquery Ajax in Codeigniter. 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 CodeigniterStep 2: Create a Database in tableStep 3: Connect to DatabaseStep 4: Create ControllerStep 5: Create a ModelStep 6: Create The View

Step 1: Download and install CodeigniterWe 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 tableIn this step, we have to create a table in the database, so we will create a database using the below code.

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

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

$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 ControllerIn 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

<?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'] = './assetshttps://www.onlinecode.org/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="https://speedassessment.com/?big=".base_url().'assetshttps://www.onlinecode.org/images/'.$image.'" width="300" height="225"  />';  
			}  
	   }  
    }  
}

?>

Step 5: Create a ModelIn 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

<?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 ViewFinally, we will create the image_upload.php file in the views directory. here in this file use below following code.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>CodeIgniter Upload Image File with preview using Jquery Ajax - onlinecode</title>
    <link rel="stylesheet" href="#">
</head>
<body>
    <div >
	    <div > 
			<div >
			<h4>Upload files using Codeigniter and Ajax</h4>
				<form  id="submit">
					<div >
						<input type="text" name="title"  placeholder="Title">
					</div>
					<div >
						<input type="file" name="file">
					</div>
					<div >
						<button  id="btn_upload" type="submit">Upload</button>
					</div>
				</form>   
			</div>
		</div>
	    <div > 
			<div  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.1https://www.onlinecode.org/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>

Please follow and like us:

Hope this code and post will helped you for implement CodeIgniter Upload Image File with preview using Jquery Ajax – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github