Home » how to upload multiple images in codeigniter?

how to upload multiple images in codeigniter?

Today, We want to share with you how to upload multiple images in codeigniter?.In this post we will show you how to upload two different files in codeigniter, hear for multiple image upload with resize in codeigniter we will give you demo and example for implement.In this post, we will learn about how to display multiple images in codeigniter with an example.

Multiple file upload in codeigniter Example

In this Article, I will share how to upload multiple image Codeigniter 3 app with you. We can easily do multiple file upload instances in codeigniter.

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

I will give you simple example step by step to upload many files in codeigniter 3 application. We will use the upload library for multiple file uploads in Codeiner. We will store all images or files in our upload directory using the upload library.

So, let’s follow a few steps to upload multiple file or picture or images examples:

Read Also:  PHP Strong Random Number and String Generator

Step 1: Download Fresh Codeigniter 3

In the first step we will download a new version of Codeigniter 3, so if you have not downloaded yet, download from here: Download Codeigniter 3.

Step 2: Add Route

In this step you have to add some route to your route file. So first we will make a path for image uploading. Insert bellow content into route file: application/config/routes.php

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


$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['image-upload'] = 'FileImgDoUpload';
$route['image-upload/post']['post'] = "FileImgDoUpload/DoUploadFileImg";

Step 3: Create Controller

In this step, we need to make an “FileImgDoUpload” controller with index () and DoUploadFileImg (). So make items in this path application / controllers / items.php. application/controllers/FileImgDoUpload.php

<?php
  
class FileImgDoUpload extends CI_Controller {
   
   /**
    * Manage __construct
    *
    * @return Response
   */
   public function __construct() { 
      parent::__construct(); 
      $this->load->helper('url'); 
   }
  
   /**
    * Manage index
    *
    * @return Response
   */
   public function index() { 
      $this->load->view('ciimgFileUplodingFrm'); 
   } 
    
   /**
    * Manage DoUploadFileImg
    *
    * @return Response
   */
   public function DoUploadFileImg() { 
   
      $data = [];
   
      $count = count($_FILES['files']['name']);
    
      for($i=0;$i<$count;$i++){
    
        if(!empty($_FILES['files']['name'][$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];
  
          $config['upload_path'] = 'uploads/'; 
          $config['allowed_types'] = 'jpg|jpeg|png|gif';
          $config['max_size'] = '5000';
          $config['file_name'] = $_FILES['files']['name'][$i];
   
          $this->load->library('upload',$config); 
    
          if($this->upload->do_upload('file')){
            $uploadData = $this->upload->data();
            $filename = $uploadData['file_name'];
   
            $data['totalFiles'][] = $filename;
          }
        }
   
      }
   
      $this->load->view('ciimgFileUplodingFrm', $data); 
   }
  
} 
?>

Step 4: Create View

In this step we will create the ciimgFileUplodingFrm.php view file. In this file we will write the design of html form using the form helper and url helper. So let’s update the file: application/views/ciimgFileUplodingFrm.php

Read Also:  Laravel Server Side Individual Column Filtering

<!doctype html>
<html>
  <head>
   <title>PHP Codeigniter - Upload Multiple Files and Images Example - Pakainfo.com</title>
  </head>
<body>
  
    <strong><?php if(isset($totalFiles)) echo "Successfully uploaded ".count($totalFiles)." files"; ?></strong>
       
    <form method='post' action='/image-upload/post' enctype='multipart/form-data'>
   
      <input type='file' name='files[]' multiple=""> <br/><br/>
      <input type='submit' value='Upload' name='upload' />
   
    </form>
   
</body>
</html>

Okay now we are ready to run the above multiple file upload in codeigniter with example, but we need to create an “upload” folder on our root directory before the run instance. All pictures will upload to the “upload” directory, so give sure permission as well.

Read Also:  How to upload image in codeigniter mysql database and display?

So let’s run the bellow command on our root directory for instant run:

php -S localhost:8000

Now you can open URL below on your browser:

http://localhost:8000/image-upload

Make sure, first you have to set the base URL on your configuration file below:

$config['base_url'] = 'http://localhost:8000/';

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about multiple file upload in codeigniter with example.
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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Woocommerce Get custom product attributes
  2. Read Also:  Simple JQuery Ajax Request Example with Laravel 6
  3. Read Also:  pagination code in php with next and previous demo
  4. Read Also:  How to add Ajax loader image in PHP?
  5. Read Also:  how to add edit and delete rows of a html table with javascript?
  6. Read Also:  Laravel 6 schema Timestamps Example
  7. Read Also:  How to make PHP Pagination With Example and Demo?
  8. Read Also:  Codeigniter drag and drop files Multiple Images Upload
  9. Read Also:  Laravel Advance Database Query Builder
  10. Read Also:  codeigniter 3 Multiple File Drag and Drop Upload

Leave a Comment

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