Codeigniter upload multiple images using drag and drop with example
In this example, I am going to tell you how to upload multiple image using dropzone library in Codeigniter application. You can drag and drop image to upload image on the server.
With the help of dropzone plugin, you can easily upload images on the server.
In most of the web application, it requires to upload bulk images with preview feature.
In HTML, there is a default feature with file type of input field with multiple but it does not look so much pretty as dropzone provides.
You can freely download the dropzone library because it is an open source javascript library.
Dropzone provides the different type of validation rules like max file upload, upload specific type of file like you can set the validation rule to take only jpeg extension.
For this example, I need to create a simple html form with the class name "dropzone" to use dropzone features and create a uploads directory to save all images.
Step 1: Download Codeigniter 3In this first step, I need to download fresh version of Codeigniter to start with scratch.
Step 2: Add RoutesIn this step, I will add two routes in my route file to handle the request to upload file on the server.
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['dropzone-upload-image'] = 'ImageController'; $route['dropzone-upload-image/post']['post'] = 'ImageController/uploadImage';
In this step, I will create a controller "ImageController.php" in following path application/controllers/.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class ImageController extends CI_Controller { public function index() { $this->load->view('dropzone_template'); } public function uploadImage() { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = 1024; $this->load->library('upload', $config); $this->upload->do_upload('file'); print_r('Image Uploaded Successfully.'); exit; } }
In this step, I will create a view file "dropzone_template.php" to render the HTML form to upload image.
application/views/dropzone_template.php<!DOCTYPE html> <html> <head> <title>Codeigniter - Upload multiple images using dropzone.js</title> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> <link href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.0.1/min/dropzone.min.css" rel="stylesheet"> <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.2.0/min/dropzone.min.js"></script> </head> <body> <div class="container"> <div class="row"> <div class="col-md-12"> <form a ction="dropzone-upload-image/post" enctype="multipart/form-data" class="dropzone" id="image-upload" method="POST"> <div> <center><h3>Click here to upload multiple image</h3></center> </div> </form> </div> </div> </div> <script type="text/javascript"> Dropzone.options.imageUpload = { maxFilesize:1, acceptedFiles: ".jpeg,.jpg,.png,.gif" }; </script> </body> </html>