How to upload image in codeigniter mysql database and display?

This image upload in codeigniter Example first of all I learn to Codeigniter is a light weight MVC based PHP framework. this is very useful in web development community. PHP Codeigniter is lots of the devloper used to perform different types of web projects. here File upload in PHP with codeigniter is very simply and in this article i will display codeigniter file upload example.

How to Upload File/image in CodeIgniter?

Our image upload in codeigniter articles are designed to create web development simple. Step-by-step instructions create our article more developer-friendly. Therefore web developers like our article rather than the official Member manual. Recently i have received many users requests for publishing the article on CodeIgniter image or file upload and fastly i have decided to publish this article.

codeigniter 3 multiple file upload, codeigniter 3 multiple image upload, multiple image upload in codeigniter, multiple file uploading in codeigniter, multiple file upload validation in codeigniter
upload multiple images in codeigniter

This article will show how to upload image in CodeIgniter using image Uploading Class. here simple learn to CodeIgniter’s image Uploading PHP Class useful to multiple files or images upload files to the server side. You can use different preferences for configuring your image upload as well as restrict some type check the type and size of the files.

For your more understand I will full demo as well as the image upload with an example. Also, I will upload the Member profile_img to the server as well as insert the Member data to the MySQL database.

Also Read This 👉   Check all checkboxes in Angular js

Step 1 : Creating Database Table (members):

Before start to writing the source code, make a sql table into your web application’s database. For this example you can make a members table into the database with some basic fields. The members table SQL would like the below.

CREATE TABLE `members` (
 `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `profile_img` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `created_at` datetime NOT NULL,
 `updated_at` datetime NOT NULL,
 `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Active, 0=Inactive',
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Step 2 : Controller (members.php):

Create a controller called members.php. in this location the following full source code in it and save this image to application/controllers/ Folders.
Member model loaded into the __construct() function for inserting Member data into the database.

first of all simply, add() function will load the Member adding form. When the form is submitted, profile_img would be uploaded and memebrs data would be inserted into the database using a Member model.

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class members extends CI_Controller
    function  __construct() {
    function add(){
            //Check whether Member upload profile_img
                $config['upload_path'] = 'uploads/images/';
                $config['allowed_types'] = 'jpg|jpeg|png|gif';
                $config['file_name'] = $_FILES['profile_img']['name'];
                //Load upload library and initialize here configuration
                    $uploadData = $this->upload->data();
                    $profile_img = $uploadData['file_name'];
                    $profile_img = '';
                $profile_img = '';
            //Prepare array of Member data
            $MemberData = array(
                'name' => $this->input->post('name'),
                'email' => $this->input->post('email'),
                'profile_img' => $profile_img
            //Pass Member data to model
            $insertMemberData = $this->Member->insert($MemberData);
            //Storing insertion status message.
                $this->session->set_flashdata('success_msg', 'Member data have been added successfully.');
                $this->session->set_flashdata('error_msg', 'Some problems occured, please try again.');
        //Form for adding Member data

We’ve used some required Preferences in our image upload configuration. You can get all the available official Preferences from click here –#preferences

Also Read This 👉   onclick image zoom jquery demo - Click/Tap To Smooth Zoom Images – Image Zoom Plugin

Step 3 : Model (Member.php):

Make a model called Member.php. Place the following code in it and save this image to application/models/ directory.
insert() function insert the provided data into the members table.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Member extends CI_Model{
    function __construct() {
        $this->tableName = 'members';
        $this->primaryKey = 'id';
    public function insert($data = array()){
            $data['created_at'] = date("Y-m-d H:i:s");
            $data['updated_at'] = date("Y-m-d H:i:s");
        $insert = $this->db->insert($this->tableName,$data);
            return $this->db->insert_id();
            return false;    

Step 4 : View (add.php):

Make a view file called add.php. location path the following all the source code in This and save this image to application/views/members/ directory.

<?php echo $this->session->flashdata('success_msg'); ?>
<?php echo $this->session->flashdata('error_msg'); ?>
<form role="form" method="post" enctype="multipart/form-data">
    <div class="panel">
        <div class="panel-body">
            <div class="dsp form-group">
                <label>Member Profile Images</label>
                <input class="form-control" type="file" name="profile_img" />
            <div class="dsp form-group">
                <label>Member Name</label>
                <input class="form-control" type="text" name="name" />
            <div class="dsp form-group">
                <label>Member Email</label>
                <input class="form-control" type="text" name="email" />
             <div class="dsp form-group">
                <input type="submit" class="btn btn-warning" name="membersubmit" value="Add">

Step 5 : Creating Upload Folder:

Make the uploads/images/ Folder to uploading the all the uploaded images.

Also Read This 👉   how to upload image in ckeditor in Laravel?


In this article, I ahve trying to display the default example of PHP CodeIgniter image upload. I hope you have learned how to upload a file/image in CodeIgniter using upload library. If you have any type of the questions about CodeIgniter image upload, let us know by commenting bellow here.