Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Codeigniter 4 Multiple Images & Files Upload Example

codeigniter 4 multiple images & files upload example

Posted on April 11, 2020December 16, 2022 By XpertPhp No Comments on codeigniter 4 multiple images & files upload example

In this tutorial, We will inform you how to upload multiple images & files in CodeIgniter 4(Codeigniter 4 multiple images & files upload example).

In this Codeigniter 4 tutorial, we will use the Codeigniter library to upload images or files. so we will give a simple example for you. let us follow the below steps.

Overview
Step 1: Download Codeigniter
Step 2: Create a Database in table
Step 3: Connect to Database
Step 4: Create Controller
Step 5: Create Routes
Step 6: Create Views Files
Step 7: Run The Application

Step 1: Download Codeigniter
If you want to download or install CodeIgniter 4 then you can below Url.
How To Install Codeigniter 4 Using Manual, Composer, Git

Step 2: Create a Database in table
In this step, We will create the database and table.

1
2
3
4
5
6
CREATE TABLE users (
    id int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key',
    name varchar(100) NOT NULL COMMENT 'Name',
    type varchar(255) NOT NULL COMMENT 'File Type',
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='users table' AUTO_INCREMENT=1;

Step 3: Connect to Database
Go to the “app/Config/Database.php” folder and open the database.php file some changes in this file like hostname, database username, database password, and database name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public $default = [
'DSN'      => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'codeigniter4_image',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug'  => (ENVIRONMENT !== 'production'),
'cacheOn'  => false,
'cacheDir' => '',
'charset'  => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre'  => '',
'encrypt'  => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port'     => 3306,
];

Step 4: Create Controller
In this step, we will create an “ImageUpload.php” controller.
app/Controllers/ImageUpload.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php namespace App\Controllers;
use CodeIgniter\Controller;
class ImageUpload extends Controller
{
    public function index()
    {    
        return view('image');
    }    
    public function upload_image()
    {    
         helper(['form', 'url']);
        
        $database = \Config\Database::connect();
        $user = $database->table('users');
$msg = 'Please select a valid files';
if ($this->request->getFileMultiple('file')) {
             foreach($this->request->getFileMultiple('file') as $file)
             {  
                $file->move(WRITEPATH . 'uploads');
              $data = [
                'name' =>  $file->getClientName(),
                'type'  => $file->getClientMimeType()
              ];
              $user->insert($data);
              $msg = 'Files has been uploaded';
             }
        }
    
      
return redirect()->to( base_url('image') )->with('msg', $msg);
    }
}
 
?>
See also  codeigniter 4 image upload example tutorial

Step 5: Create Routes
Add the following route code in the “app/config/Routes.php” file.

1
$routes->get('/image', 'ImageUpload::index');

Step 6: Create Views Files
Finally, we will create an image.php in the app/views directory.
app/views/image.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!DOCTYPE html>
<html lang="en">
<head>
<title>codeigniter 4 multiple images & files upload example - XpertPhp</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<?php if (session('msg')) : ?>
<div class="alert alert-info alert-dismissible">
<?= session('msg') ?>
<button type="button" class="close" data-dismiss="alert"><span>×</span></button>
</div>
<?php endif ?>
<form method="post" action="<?php echo base_url('ImageUpload/upload_image');?>" enctype="multipart/form-data">
  <div class="form-group">
<label>Image</label>
<input type="file" name="file[]" class="form-control">
  </div>
  <button type="submit" class="btn btn-primary">Upload</button>
</form>
</div>
</body>
</html>

Step 7: Run The Application
We can start the server and run this example using the below command.

1
php spark serve

Now we will run our example using the below Url in the browser.

1
http://localhost:8080/image

Codeigniter Tags:codeigniter 4 image upload, Codeigniter 4 Tutorial

Post navigation

Previous Post: Codeigniter 4 Generate PDF Example Tutorial
Next Post: Codeigniter 4 Login and Registration Example

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Angular 15 CRUD Application Example Tutorial
  • Laravel 10 Form Validation Example Tutorial
  • Angular 15 Custom Form Validation Example
  • Laravel 10 Send Email Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com