how to upload multiple separate images in codeigniter with database?

Today, We want to share with you multiple image upload in codeigniter with database.In this post we will show you how to upload 2 separate images in codeigniter, hear for update multiple image in codeigniter we will give you demo and example for implement.In this post, we will learn about Codeigniter Drag And Drop Files Multiple Images Upload with an example.

multiple image upload validation in codeigniter

You only need to set the allowed_types preference in Upload library.

$config['allowed_types'] = '*';

Database Table Creation

codeigniter_db

CREATE TABLE `files` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `file_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `created` datetime NOT NULL COMMENT 'Upload Date',
 `updated_at` datetime NOT NULL,
 `is_active` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Unblock, 0=Block',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Upload Folder Creation

Controller (Upload_files.php)

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Upload_Files extends CI_Controller
{
    function  __construct() {
        parent::__construct();
        $this->load->model('file');
    }
    
    function index(){
        $data = array();
        if($this->input->post('fileSubmit') && !empty($_FILES['accountProfiles']['name'])){
            $filesCount = count($_FILES['accountProfiles']['name']);
            for($i = 0; $i < $filesCount; $i++){
                $_FILES['accountProfile']['name'] = $_FILES['accountProfiles']['name'][$i];
                $_FILES['accountProfile']['type'] = $_FILES['accountProfiles']['type'][$i];
                $_FILES['accountProfile']['tmp_name'] = $_FILES['accountProfiles']['tmp_name'][$i];
                $_FILES['accountProfile']['error'] = $_FILES['accountProfiles']['error'][$i];
                $_FILES['accountProfile']['size'] = $_FILES['accountProfiles']['size'][$i];

                $uploadPath = 'plugins/files/';
                $config['upload_path'] = $uploadPath;
                $config['allowed_types'] = 'gif|jpg|png';
                
                $this->load->library('upload', $config);
                $this->upload->initialize($config);
                if($this->upload->do_upload('accountProfile')){
                    $fileData = $this->upload->data();
                    $profileInformation[$i]['file_name'] = $fileData['file_name'];
                    $profileInformation[$i]['created'] = date("Y-m-d H:i:s");
                    $profileInformation[$i]['updated_at'] = date("Y-m-d H:i:s");
                }
            }
            
            if(!empty($profileInformation)){
                
                $insert = $this->file->insert($profileInformation);
                $displayMessage = $insert?' Good Luck Files uploaded successfully.':' sorry Some problem occurred, please try again.';
                $this->session->set_flashdata('displayMessage',$displayMessage);
            }
        }
        //Get files data from database
        $data['files'] = $this->file->getRows();
        //Pass the files data to view
        $this->load->view('upload_files/index', $data);
    }

}

Model (File.php)

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class File extends CI_Model{

    public function getRows($id = ''){
        $this->db->select('id,file_name,created');
        $this->db->from('files');
        if($id){
            $this->db->where('id',$id);
            $query = $this->db->get();
            $result = $query->row_array();
        }else{
            $this->db->order_by('created','desc');
            $query = $this->db->get();
            $result = $query->result_array();
        }
        return !empty($result)?$result:false;
    }
    
    public function insert($data = array()){
        $insert = $this->db->insert_batch('files',$data);
        return $insert?true:false;
    }
    
}

View

(upload_files/index.php)

<div class="container">
    <div class="row">
        <p><?php echo $this->session->flashdata('displayMessage'); ?></p>
        <form enctype="multipart/form-data" action="" method="post">
            <div class="form-group">
                <label>Select Files</label>
                <input type="file" class="form-control" name="accountProfiles[]" multiple/>
            </div>
            <div class="form-group">
                <input class="form-control" type="submit" name="fileSubmit" value="UPLOAD"/>
            </div>
        </form>
    </div>
    <div class="row">
        <ul class="gallery">
            <?php if(!empty($files)): foreach($files as $file): ?>
            <li class="member">
                <img src="<?php echo base_url('plugins/files/'.$file['file_name']); ?>" alt="" >
                <p>Uploaded On <?php echo date("j M Y",strtotime($file['created'])); ?></p>
            </li>
            <?php endforeach; else: ?>
            <p>Image(s) not found.....</p>
            <?php endif; ?>
        </ul>
    </div>
</div>

Config (autoload.php)

$autoload['libraries'] = array('session','database');

$autoload['helper'] = array('url');

By accessing the index method of the Upload_Files controller (http://localhost/codeigniter/upload_files) on the browser

I hope you get an idea about Upload Multiple Files And Images In CodeIgniter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.