How to Upload files Using CodeIgniter Framework

Uploading files is one the most common functionalities you will see in most web applications.

Manual configuration and initialization of a file upload is quite tedious but with a framework alot of work is reduced to help develop application real quick.

Codeigniter framework provides a descent and efficient way to do this

The file upload library included in CodeIgniter is very simple to implement and highly configurable.

In this blog post I’m going to show you a basic implementation of it specifically for beginners.

Upload files Using CodeIgniter Framework

Just like for any framework, CodeIgniter follows the MVC Architecture.

Model: Handles the Database Queries

View: Handles the User Interface for the User

Controller: Handles the information to be showed to the User.

Step 1: Setup the View

Firstly, to upload a file we need to prepare a simple form view with an input field to upload a file.

<form action="" method="POST" enctype="multipart/form-data" >
    Select File To Upload:<br />
    <input type="file" name="userfile"  />
    <br /><br />
    <input type="submit" name="submit" value="Upload" class="btn btn-success" />

Please Note:

  • We have used ‘enctype’ attribute (means ‘ENCode Type’) with a value of “multipart/form-data” in ‘form’ tag, which tells CodeIgniter that, this form will contain a file upload control.
  • A file browser control is added by specifying type=’file’. This is the main control where user will be able to select a file from his local computer. By default user will be able to select single file. But if you wish to facilitate multiple files on your web application, you can additionally use “multiple=’multiple’” attribute in this tag. This will do the trick well.
  • When uploading files using CodeIgniter the “name” attribute should always be userfile.

Step 2 : Define File Configuration.

Once a file is uploaded, we let out our server-side codes to kick in and configure the uploaded file.

First , we will need to make a configuration array.

This will include the basic settings for file upload like upload path, maximum size, allowed file types etc.

You will need to initialize different configuration arrays for different upload forms.

If the file upload of your application works different from each other, you are better off creating the configuration array in the respective controller.

In other case, where if the file upload of your application works exactly same for all file uploads, then you can specify the configuration in ‘application/config’ directory and load when needed.

$this->config =  array(
                  'upload_path'     => dirname($_SERVER["SCRIPT_FILENAME"])."/files/",
                  'upload_url'      => base_url()."files/",
                  'allowed_types'   => "gif|jpg|png|jpeg|pdf|doc|xml",
                  'overwrite'       => TRUE,
                  'max_size'        => "1000KB",
                  'max_height'      => "768",
                  'max_width'       => "1024"  

Step 3: Loading CodeIgniter File Upload Library

CodeIgniter has a lot of helpful libraries that we can use for rapid application development.

Like said, file upload can be messy with Core PHP, depending on the complexity.

The CodeIgniter File Upload Library, makes this a lot easy and the configuration array you made above will be used as the settings for the library to function.

So we’ll load the library and use the do_upload() function to upload the file, using the settings specified in the configuration array.

$this->load->library('upload', $this->config);
    echo "file upload success";
   echo "file upload failed";

If the code above returns any error, you can show the error messages using “display_errors()” function of the library.

After successful file upload, CodeIgniter returns an object that has all the properties of the uploaded file for further usage, for example, inserting a record in database, making a thumbnail etc.

The final result would look like below.


Thank you for reading till the end of this post. I hope i was able to help you out with uploading a file using CodeIgniter.

Let me know your comments, thought, opinions or any problems if you are facing with CodeIgniter File Upload. I’ld be happy to help you out.










Published by


Generalist with a passion for web dev, cloud computing and Internet Marketing. Working as System Administrator in Melbourne.

Leave a Reply