Codeigniter Multiple Files Upload Example – onlinecode

Codeigniter Multiple Files Upload Example – onlinecode

In this post we will give you information about Codeigniter Multiple Files Upload Example – onlinecode. Hear we will give you detail about Codeigniter Multiple Files Upload Example – 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 Multiple files or multiple images with Codeigniter Framework. whenever we are uploading many images at that time we use the form_open_multipart.

first, we will take the form and some designing with bootstrap and we will also use multiple name attributes for the upload multiple images.

Codeigniter Multiple Files Upload Example, rename image in Codeigniter, multiple images upload, image upload, multiple files upload

Overview

Step 1:Create Upload Directory

Step 2:Create The Controller

Step 3:Create The Model

Step 4:Create The View

Step 1: Create Upload Directory

Now, we will create assets/uploads/ directory on own project directory.

Step 2: Create The Controller

After then, we will create the Multipleimage.php controller in the controller directory.

Here first, we will load the model and helper URL. when the user submits the form and uploads images that time we will check the condition and count how many images upload. we will be uploading images using the for loop. we rename all images using the PATHINFO_EXTENSION and pathinfo.

PHP
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Multipleimage extends CI_Controller {

  public function __construct() {
    parent::__construct();

    $this->load->model('Image_model');
    $this->load->helper('url');
  }

  public function index(){

    if ($this->input->post('submit') && $_FILES['images']['name'] != '') {
      $data = array();

      $count_images = count($_FILES['images']['name']);
 
      for($i=0;$i<$count_images;$i++){
 
        if(!empty($_FILES['images']['name'][$i])){
		  $ext = pathinfo($_FILES['images']['name'][$i], PATHINFO_EXTENSION);
		  $name = 'image' . rand(100000, 999999) . date("Y-m-d") . '.' . $ext;	
          $_FILES['file']['name'] = $name;
          $_FILES['file']['type'] = $_FILES['images']['type'][$i];
          $_FILES['file']['tmp_name'] = $_FILES['images']['tmp_name'][$i];
          $_FILES['file']['error'] = $_FILES['images']['error'][$i];
          $_FILES['file']['size'] = $_FILES['images']['size'][$i];

          $config['upload_path'] = './assets/uploads/'; 
          $config['allowed_types'] = 'jpg|jpeg|png|gif';
          $config['max_size'] = '10000';
          $config['file_name'] = $_FILES['images']['name'][$i];
 
          $this->load->library('upload',$config); 
 
          if($this->upload->do_upload('file')){
            $uploadData = $this->upload->data();
			$arrData["image_name"] =$name;
            $this->Image_model->save_image($arrData);
          }
        }
 
      }
    }
      $this->load->view('add');
  }
}
?>

Step 3: Create The Model

Now, we will create the Image_model.php model in the model directory.

PHP
<?php 

defined('BASEPATH') OR exit('No direct script access allowed');

   Class Image_model extends CI_Model
   {
       function save_image($arrData)
       {
           if ($this->db->insert('file_upload_image',$arrData)) {
               return true;
           } else {
               return flase;
           }
       }
	}
?>

 

Step 4: Create The View

Finally, we will create the image.php file in the views directory. here in this file use below following code.

PHP
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Codeigniter Multiple Files Upload Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="#">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0https://www.onlinecode.org/js/bootstrap.min.js"></script>
</head>
<body>
<div >
    <div >
        <div >
            <section >
			  <header >
				  <h1 >Codeigniter Multiple Files Upload Example</h1>
			  </header>
                <div >
                  <div >
                      <div >
                          <?php
                          $attributes = array('name' => 'frmimage', 'id' => 'frmimage', 'class' => 'imageform form-horizontal adminex-form');

                          echo form_open_multipart('',$attributes);
                          ?>
                          <div >
                              <div >
                                  <input type="file" name="images[]" multiple >
                              </div>
                          </div>
                          <div >
                              <div >
                                  <?php
                                  $datasubmit = array(
                                      'name'        => 'submit',
                                      'id'          => 'submit',
                                      'class'		=> 'btn btn-success',
                                      'value'		=> 'File Upload'
                                  );
                                  echo form_submit($datasubmit);
                                  ?>
                              </div>
                          </div>
                      </div>
                      <?php echo form_close(); ?>
                  </div>
              </div>			
          </section>
      </div>
  </div>
</div>
</body>
</html>

Please follow and like us:

Hope this code and post will helped you for implement Codeigniter Multiple Files Upload Example – 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

Leave a Comment

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

7  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US