CodeIgniter File Uploading

CodeIgniter File Uploading Class provides various preferences like upload_path,file_name, max_size,max_width,max_height and max_filename.

File Upload Example

We will use following step to Uploading a file in CodeIgniter.

Step 1

Create view file inside application\views\uploadform.php for uploading file.

<title>Codeigniter Upload </title>
<h3>Codeigniter Upload </h3>
<?php print_r($error); ?>
<form action="<?php echo base_url();?>ciupload/upload_file" method="post"  enctype="multipart/form-data">
<input type="file" name="uploadfile" size="20" />
<input type="submit" value="upload" />
Step 2

Create view file inside application\views\uploadsuccess.php to show success message after upload.

<title>Codeigniter Upload </title>
<h3>Your file was successfully uploaded!</h3>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
Step 3

Create Controller file inside application/controllers/ciupload.php.

class upload extends CI_Controller{
  function __construct()

function index()
   $this->load->view('uploadform',array('error' => ' ' ));
function upload_file()
   $config['upload_path'] = './uploads/';
    // Uploading all type of file
    $config['allowed_types'] = '*';
    $this->load->library('upload', $config);
    if (! $this->upload->do_upload($userfile))
      $data['error'] = array('error' => $this->upload->display_errors());

      $data = array('upload_data' => $this->upload->data($userfile));

      $this->load->view('uploadsuccess', $data);
Step 4

Create a folder at the root of your CodeIgniter installation called uploads and set uploads folder permissions to 777.

Step 5

We can set the following preferences for file upload in our controller.

$config['upload_path'] = './uploads/';
//Path of the folder in which we upload file. 
$config['allowed_types'] = 'gif|jpg|png';
//Types of files that we can upload.
$config['max_size'] = '100';
//The maximum file size (in kilobytes) that we can upload.Set to zero for no limit 
$config['max_width'] = '1024';
//The maximum file  width (in pixels) that we can upload.Set to zero for no limit 
$config['max_height'] = '768';
//The maximum file  height (in pixels) that we can upload.Set to zero for no limit
$this->load->library('upload', $config); 

Function Reference for CodeIgniter File Upload Class


This function is used for uploading file in codeigniter.Form must be a "multipart type"

<form method="post" action="action" enctype="multipart/form-data" />

File name should be userfile if want to change it simply pass its value to the do_upload function like given below.

$field_name = "fieldName"; $this->upload->do_upload($field_name)


This function is used for retrieve error messages if do_upload() function returned false.


This is function returns an array containing all of the data related to the uploaded file.Array prototype are given below.

    [file_name]    => logo.jpg
    [file_type]    => image/jpeg
    [file_path]    => /path/to/your/upload/
    [full_path]    => /path/to/your/upload/jpg.jpg
    [raw_name]     => logo
    [orig_name]    => logo.jpg
    [client_name]  => logo.jpg
    [file_ext]     => .jpg
    [file_size]    => 11.2
    [is_image]     => 1
    [image_width]  => 500
    [image_height] => 700
    [image_type]   => jpeg