I will show you how to upload multiple files using Codeigniter, AJAX and jQuery without page refresh. This multiple files upload tutorial example is very helpful to implement the upload functionality. In this example the files are selected using the browse button and files are uploaded to the ‘uploads’ directory. Codeigniter controller stores files into the defined location and returns response as success or failure messages from the controller. As a validation step I have added only to check if you have selected a file for uploading or not.

You may read also AJAX File Upload using Codeigniter, jQuery, AJAX Multiple Files Upload using PHP, jQuery and  AJAX File Upload using PHP, jQuery


Netbeans 8.1
XAMPP in Windows
Codeigniter 3.0.6

Configure XAMPP and Netbeans

From Netbeans IDE go to Tools->Options. Click on PHP. Now on tab “General” browse the file for “PHP 5 Interpreter”. The php interpreter file generally placed inside the <physical drive in Windows OS>:xampp\php\php.exe

Configure Codeigniter and Netbeans

Create a new PHP project in Netbeans. Then remove the index.php file from the newly created project. Now copy the extracted files from Codeigniter 3.0.6 to the newly created project directory.

Directory Structure

A typical directory structure for the project would be as shown below. Here assests directory will contain static resources like css, js, images. uploads directory will hold all uploaded files.

ajax file upload

Step 1. Now modify <root directory>/application/config/autoload.php file for auto-loading html, url, file, form

$autoload['helper'] = array('html', 'url', 'file', 'form');

Step 2. Create a controller file ajaxupload.php under ci3/application/controllers with the following source code


if (!defined('BASEPATH'))
    exit('No direct script access allowed');

 * Description of ajaxupload
 * @author https://roytuts.com
class AjaxUpload extends CI_Controller {

    function __construct() {

    function index() {
        $this->load->view('files_upload_ajax', NULL);

    function upload_files() {
        if (isset($_FILES['files']) && !empty($_FILES['files'])) {
            $no_files = count($_FILES["files"]['name']);
            for ($i = 0; $i < $no_files; $i++) {
                if ($_FILES["files"]["error"][$i] > 0) {
                    echo "Error: " . $_FILES["files"]["error"][$i] . "<br>";
                } else {
                    if (file_exists('uploads/' . $_FILES["files"]["name"][$i])) {
                        echo 'File already exists : uploads/' . $_FILES["files"]["name"][$i];
                    } else {
                        move_uploaded_file($_FILES["files"]["tmp_name"][$i], 'uploads/' . $_FILES["files"]["name"][$i]);
                        echo 'File successfully uploaded : uploads/' . $_FILES['files']['name'][$i] . ' ';
        } else {
            echo 'Please choose at least one file';


Step 3. Create a view file files_upload_ajax.php under ci3/application/views

<!DOCTYPE html>
        <meta charset="UTF-8">
        <title>AJAX File upload using Codeigniter, jQuery</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function (e) {
                $('#upload').on('click', function () {
                    var form_data = new FormData();
                    var ins = document.getElementById('multiFiles').files.length;
                    for (var x = 0; x < ins; x++) {
                        form_data.append("files[]", document.getElementById('multiFiles').files[x]);
                        url: 'http://localhost/ci3/index.php/ajaxupload/upload_files', // point to server-side controller method
                        dataType: 'text', // what to expect back from the server
                        cache: false,
                        contentType: false,
                        processData: false,
                        data: form_data,
                        type: 'post',
                        success: function (response) {
                            $('#msg').html(response); // display success response from the server
                        error: function (response) {
                            $('#msg').html(response); // display error response from the server
        <p id="msg"></p>
        <input type="file" id="multiFiles" name="files[]" multiple="multiple"/>
        <button id="upload">Upload</button>

Step 4. Modify file ci3/application/config/routes.php file

$route['default_controller'] = 'ajaxupload';

Step 5. If everything is fine then run the application. You will get the output in the browser.

If you try to upload file without selecting any file then you would see error message “Please select at least one file”.

If the file successfully uploaded then you will see success message “File successfully uploaded : uploads/<some-file-name>“.

ajax multiple files upload codeigniter jquery

If the file you are trying to upload already exists then you will see a message “File already exists : uploads/<some-file-name>“.

Hope you have got an idea how to upload multiple files using Codeigniter, AJAX and jQuery.

Thanks for reading.

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials Twitter Facebook  Google Plus Linkedin Or Email Me

Leave a Reply

Your email address will not be published. Required fields are marked *