CodeIgniter 4 Drag and Drop File Upload Using Dropzone

Share this Article
Reading Time: 6 minutes

In many applications, you have been observed file upload process is bit attractive. Inside this article we will see the concept of CodeIgniter 4 drag and drop file upload using dropzone jquery plugin.

Dropzone.js is a jquery plugin, dropzone.js through we can select one by one image and also with preview.

Uploading files in application using dropzone plugin is super easy. This tutorial is very interesting and easy to learn.

We can upload any type of file using dropzone but for this tutorial we will use the feature to upload only images via dropzone jquery plugin.

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s get started.

Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system.

To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.

Turn Development Mode On

When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

 // Do it to 
CI_ENVIRONMENT = development

Now application is in development mode.

Create Database & Tables To Application

We need to create a database. For database we will use MySQL. We have 2 options available to create database. Either we can use PhpMyAdmin Manual interface Or we can use command to create.

CREATE DATABASE codeigniter4_app;

Successfully, we have created a database.

Next, we need to create a table inside database.

CREATE TABLE image_uploads (
  id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  filename text COLLATE utf8mb4_unicode_ci NOT NULL,

Database Connectivity to Application

Open .env file from project root. Search for DATABASE. You should see the connection environment variables.

Let’s set the value for those to connect with database.

 database.default.hostname = localhost
 database.default.database = codeigniter4_app
 database.default.username = root
 database.default.password = root
 database.default.DBDriver = MySQLi

Now, database successfully connected with application.

Create Model

Open project into terminal and run this spark command to create model file.

$ php spark make:model ImageUpload

It will create ImageUpload.php file at /app/Models folder.

Open ImageUpload.php and write this complete code into it.


namespace App\Models;

use CodeIgniter\Model;

class Imageupload extends Model
	protected $DBGroup              = 'default';
	protected $table                = 'image_uploads';
	protected $primaryKey           = 'id';
	protected $useAutoIncrement     = true;
	protected $insertID             = 0;
	protected $returnType           = 'array';
	protected $useSoftDelete        = false;
	protected $protectFields        = true;
	protected $allowedFields        = [

	// Dates
	protected $useTimestamps        = false;
	protected $dateFormat           = 'datetime';
	protected $createdField         = 'created_at';
	protected $updatedField         = 'updated_at';
	protected $deletedField         = 'deleted_at';

	// Validation
	protected $validationRules      = [];
	protected $validationMessages   = [];
	protected $skipValidation       = false;
	protected $cleanValidationRules = true;

	// Callbacks
	protected $allowCallbacks       = true;
	protected $beforeInsert         = [];
	protected $afterInsert          = [];
	protected $beforeUpdate         = [];
	protected $afterUpdate          = [];
	protected $beforeFind           = [];
	protected $afterFind            = [];
	protected $beforeDelete         = [];
	protected $afterDelete          = [];

Create Controller

Next, we need to create a controller file.

$ php spark make:controller Dropzone --suffix

It will create a file DropzoneController.php at /app/Controllers folder.

Open controller file and write this complete code into it.


namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\Imageupload;

class DropzoneController extends BaseController
	public function dropzone()
        return view('upload-view');
	public function dropzoneStore()
        $image = $this->request->getFile('file');

        $imageName = $image->getName();

        $image->move('images', $imageName);

		$imageUpload = new ImageUpload();
		$data = [
			"filename" => $imageName


        return json_encode(array(
			"status" => 1,
			"filename" => $imageName

Create Routes

Open Routes.php from /app/Config folder.

$routes->get("image-upload", "DropzoneController::dropzone");
$routes->post("dropzone/upload", "DropzoneController::dropzoneStore");

Create Layout File

Next, need to create upload-view.php inside /app/Views folder.

Open this view file and write this updated code.

<!DOCTYPE html>

    <title>CodeIgniter 4 Drag And Drop File Upload Using Dropzone - Online Web Tutor</title>
    <link rel="stylesheet" href="">
    <script src=""></script>
    <link href="" rel="stylesheet">
    <script src=""></script>


    <div class="container section">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <h3 class="text-center">CodeIgniter 4 Drag And Drop File Upload Using Dropzone</h3>
                <form action="<?= base_url('dropzone/upload') ?>" method="POST" enctype="multipart/form-data" class="dropzone" id='image-upload'>
                        <h3 class="text-center">Upload Multiple Image By Click On Box</h3>
    <script type="text/javascript">
        Dropzone.options.imageUpload = {
            maxFilesize: 1,
            acceptedFiles: ".jpeg,.jpg,.png,.gif",


Application Testing

Start development server:

$ php spark serve

URL: http://localhost:8080/image-upload

This is the view we will get before upload.

Let’s upload few images by dragging and dropping into area.

Data has been saved to database table as well.

We hope this article helped you to learn about CodeIgniter 4 Drag and Drop File Upload Using Dropzone Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on CodeIgniter 4 here