Codeigniter File Upload

Codeigniter File Upload: Codeigniter provides uploading classes to upload the files. It provides several options to set restrictions(such as file size, file type) and preferences of the file. Here in this tutorial we are going to explain how you can upload files/Images in Codeigniter.

Codeigniter File Upload | Image Upload Library Example

Let us go step by step to understand how to upload files/images in Codeigniter.

Steps to Upload Files

Following steps are main steps to upload files in codeigniter-

  • Upload Form(Html View)
  • Controller
  • Success Page(Html View)

Upload Form(Html View)

Create a view file as upload_form_view.php in application/views folder. This Html Form contains the form input to select and upload the file –

Codeigniter File Upload | Html Part Example:

<title>Upload File Form</title>

<?php echo $error;?>

<?php echo form_open_multipart('upload/process_upload');?>

<input type="file" name="myfile" />

<input type="submit" value="Upload File" />




Create a controller file as upload.php in application/controllers folder. This will contain the code to upload file on server as below-

Codeigniter File Upload Controller Example:


class Upload extends CI_Controller {

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

        public function index()
                $this->load->view('upload_form_view', array('error' => ' ' ));

        public function process_upload()
                $config['upload_path']          = './uploads/';
                $config['allowed_types']        = 'gif|jpg|png';
                $config['max_size']             = 2048;
                $config['max_width']            = 1000;
                $config['max_height']           = 800;
                $this->load->library('upload', $config);

                if ( ! $this->upload->process_upload('myfile'))
                        $error = array('error' => $this->upload->display_errors());

                        $this->load->view('upload_form_view', $error);
                        $data = array('upload_data' => $this->upload->data());

                        $this->load->view('success_view', $data);


In controller method process_upload $config array is used to set the preferences and restrictions. Let us have look on the config –

  • $config[‘upload_path’] : This is used to set the directory path where images should be uploaded.
  • $config[‘max_size’] : This is used to set the maximum file size allowed to be uploaded.
  • $config[‘allowed_types’] : This is used to set the file types that will be allowed for upload for example jpg, png etc.
  • $config[‘max_width’] : This will set the max with of file allowed.
  • $config[‘max_height’] : This will set the max height of file allowed.

Success Page(Html View)

Create a view file as success_view.php in application/views folder. This view will be loaded when the file is uploaded successfully.

Success Page View:

<title>Upload Success</title>

<h3>The 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 Other File!'); ?></p>



Add Comment

📖 Read More