This post is about CodeIgniter(C.I.) file upload. By the use of file uploading class, you can easily upload a file or an image.

One can easily validate and restrict file type, its size and can even provide various preferences while uploading a file or image.

Using CodeIgniter Image Upload example, we will teach you how an image or a file gets uploaded to an upload folder or a targeted location.

Below is our complete code with live demo and download code option.


 Watch the live demo or download code from the link given below

Note: Download the file and extract it to the root file of your local server.


Firstly, you’ll need to create a destination folder where you want your images to get uploaded.

So, Create a folder named “uploads” at the root of your CodeIgniter(CI) installation.

Now create two “view” files file_view.php and upload_success.php in a views folder of CodeIgniter . First file will display a complete form that contains upload field and the second file is to display the message “successfully uploaded”.

In your controller folder create a file naming: upload_controller.php

where we will going to load a library to  initialize an Upload class using the below code:


Also you have to set preferences for image and file i.e. size, type etc. based on that you can allow uploading. We have set file preferences in a function do_upload() of controller which looks like:

$config['upload_path'] = './uploads/';

$config['allowed_types'] = 'gif|jpg|png';

$config['max_size'] = '100';

$config['max_width'] = '1024';

$config['max_height'] = '768';


Now, you can also try it at your end by using similar URL


and pasting the complete code as shown below in your “views” and “controller” files.


Note: File uploads require a multipart form so we have include a form helper in controller to create the opening form tag as it create proper syntax you can also go for simple HTML form tags.

Syntax for form helper: –

$this->load->helper(array('form', 'url'));


Tutorial Scripts in detail

Below are the details of the code used in this tutorial with proper explanation.

View file for viewing complete form : file_view.php

<title>Upload Form</title>
<?php echo $error;?> <!-- Error Message will show up here -->
<?php echo form_open_multipart('upload_controller/do_upload');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>


Controller file : upload_controller.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload_Controller extends CI_Controller {
public function __construct() {
public function file_view(){
$this->load->view('file_view', array('error' => ' ' ));
public function do_upload(){
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
$this->load->library('upload', $config);
$data = array('upload_data' => $this->upload->data());
$error = array('error' => $this->upload->display_errors());
$this->load->view('file_view', $error);

Note : You can change preferences depending upon the file you wish to upload.

'allowed_types' => "gif|jpg|jpeg|png|iso|dmg|zip|rar|doc|docx|xls|xlsx|ppt|pptx|csv|ods|odt|odp|pdf|rtf|sxc|sxi|txt|exe|avi|mpeg|mp3|mp4|3gp",


View file to display success message : upload_success.php

<title>Upload File Specification</title>
<h3>Your file was successfully uploaded!</h3>
<!-- Uploaded file specification will show up here -->
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
<p><?php echo anchor('upload_controller/file_view', 'Upload Another File!'); ?></p>



So in this tutorial we have focused on one of the topic of codeigniter which might have beneficial for you to understand it’s complete functionality. Keep reading our blog posts for getting in touch with more coding tricks.