Codeigniter - Upload Multiple File and Image Example

By Hardik Savani | June 4, 2019 | | 1764 Viewer | Category : PHP Codeigniter


In this tutorial, i will share with you how to upload multiple image in codeigniter 3 application. we can easily do multiple file upload example in codeigniter.

i will give you simple example with step by step to upload multiple file in codeigniter 3 application. we will use upload library for multiple file upload in codeigniter. using upload library we will store all images or files store in our uploads directory.

So, let's follow few bellow step to upload multiple file or images example:

Step 1: 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.

Step 2: 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";

Step 3: 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);

}

}

?>

Step 4: 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 want me hire for your Project Work? Then Contact US.


We are Recommending you: