How to upload multiple file in codeigniter ?

How to Upload multiple images in codeigniter

In this post ,We are learn how to upload multiple images using codeigniter 3 application. and stores images in perticular folder and database .

 

1.Download codeigniter 3 Application

In This Step We are going to download codeigniter 3 application from codeigniter’s officials website
 

2 Create Controller for upload multiple files

In this steps, We are creating controller for upload multiple files in codeigniter 3 application,
 
 
$errorUploadType = $statusMsg = ''; 
            if(!empty($_FILES['files']['name']) && count(array_filter($_FILES['files']['name'])) > 0){
                $filesCount = count($_FILES['files']['name']);
                for($i = 0; $i < $filesCount; $i++){ 
                    $_FILES['file']['name']     = $_FILES['files']['name'][$i]; 
                    $_FILES['file']['type']     = $_FILES['files']['type'][$i]; 
                    $_FILES['file']['tmp_name'] = $_FILES['files']['tmp_name'][$i]; 
                    $_FILES['file']['error']    = $_FILES['files']['error'][$i]; 
                    $_FILES['file']['size']     = $_FILES['files']['size'][$i]; 
                     
                   
                    $uploadPath = 'Uploads/'; 
                   
                    $config['upload_path'] = $uploadPath; 
                    $config['allowed_types'] = 'jpg|jpeg|png|gif'; 
   
                    $this->load->library('upload', $config); 
                    $this->upload->initialize($config); 
                     
                    if($this->upload->do_upload('file')){ 
                        // Uploaded file data
                        $fileData = $this->upload->data(); 
                        $uploadData[$i]['file_name'] = $fileData['file_name']; 
                        $uploadData[$i]['uploaded_on'] = date("Y-m-d H:i:s"); 
                        $uploadData[$i]['tour_id'] = $unique;
                        $uploadData[$i]['img_id'] = $unique;
                        $uploadData[$i]['yacht_id'] = $unique;
                    }else{  
                        $errorUploadType .= $_FILES['file']['name'].' | ';
                    } 
                } 
                 
                $errorUploadType = !empty($errorUploadType)?'<br/>File Type Error: '.trim($errorUploadType, ' | '):'';
                if(!empty($uploadData)){
                    $insert = $this->file->insert($uploadData);

                    $yacht['yacht_name'] = $this->input->post('yacht_name');
                    $yacht['location'] = $this->input->post('location');
                    $yacht['price'] = $this->input->post('price');
                    $yacht['description'] = $this->input->post('description');
                    $yacht['thumbnail'] = $fileData['file_name'];

                    // print_r($fileData['file_name']);
                    // die();

                    $result0=$this->Admin_Model->insert_yacht($yacht);

                }
            }   
 

3. Creating Model For upload Multiple Images in codeigniter 3

In this step we are creating model for uploading multiple images in codeigniter 3 Application.
$config['stripe_key'] = 'pk_test_ghjfndghfht';
$config['stripe_secret'] = 'sk_test_fgdhesateju';<?php  
defined('BASEPATH') OR exit('No direct script access allowed'); 
 
class File extends CI_Model{ 
    function __construct() { 
        $this->tableName = 'files'; 
    } 
     


    public function getRows($id = ''){
        $this->db->select('id,file_name,uploaded_on'); 
        $this->db->from('files'); 
        if($id){ 
            $this->db->where('id',$id); 
            $query = $this->db->get(); 
            $result = $query->row_array(); 
        }else{ 
            $this->db->order_by('uploaded_on','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;
    } 
}

4. Create View for upload Multiple files in codeigniter 3

     <div class="form-group row">
          <label class="col-sm-3 form-control-label">Images</label>
            <div class="col-sm-9">
              <input type="file" id="upload_file" name="files[]" onchange="preview_image();" class="form-control" multiple>
              <div id="image_preview" style="display:flex; justify-content:space-between;"></div>
            </div>
        </div>

Leave a Reply

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

Back To Top