I will let you know multiple image upload in codeigniter with simple example. you can learn php codeigniter upload multiple images and store to database. we will use upload library for upload multiple files in codeigniter.
We almost need to implement multiple images or files feature in our project. so you can simply understand and you can implement your existing project with multiple file upload.
So, let's follow few bellow step to upload multiple file or images example:
Download Fresh Codeigniter 3
In First step we will download fresh version of Codeigniter 3, so if you haven't download yet then download from here : Download Codeigniter 3.
Add Route
In this step you have to add some route in your route file. So first we will create route for image uploading example.so put the bellow content in 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'] = 'ImageUpload';
$route['image-upload/post']['post'] = "ImageUpload/uploadImage";
Create Controller
In this step, we have to create "ImageUpload" controller with index() and uploadImage(). so create Items.php file in this path application/controllers/Items.php and put bellow code in this file:
application/controllers/ImageUpload.php
<?php
class ImageUpload 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('imageUploadForm');
}
/**
* Manage uploadImage
*
* @return Response
*/
public function uploadImage() {
$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('imageUploadForm', $data);
}
}
?>
Create View
In this step we will create imageUploadForm.php view file . In this file we will write design of html form using form helper and url helper. So let's update following file:
application/views/imageUploadForm.php
<!doctype html>
<html>
<head>
<title>Codeigniter - Upload Multiple Files and Images Example - ItSolutionStuff.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>
Ok Now we are ready to run Above example, But We have to create "uploads" folder on your root directory before run example. all images will upload on "uploads" directory so make sure permission too.
So let's run bellow command on your root directory for quick run:
php -S localhost:8000
Now you can open bellow URL on your browser:
http://localhost:8000/image-upload
Make Sure, First you have to set base URL on your bellow configuration file :
application/config/config.php
$config['base_url'] = 'http://localhost:8000/';
I hope it can help you...
Download Source Code form here: Download From Github
Do you like below Tutorials ?
- PHP Laravel 5.5 - import export data into excel and csv using maatwebsite
- Laravel 7.x and 6.x create custom facade example from scratch
- Node JS - Get Real time online users counter using socket io example
- PHP Laravel 7.x and 6.x - Bootstrap Admin Theme Integration example from scratch
- MySQL Query - How to select records of today to next Sunday ?
- Laravel 7.x and 6.x - username custom validation example
- Laravel 5.5 - Import Extract zip file using chumper/zipper package
- PHP Laravel Set Custom Validation Error Messages Example