CodeIgniter File Upload and Validation

Hello Readers,

I would like to explain how to upload files to server in CodeIgniter and to validate it with Form Validation library.

Here we are going to see

  1. How to upload files to server
  2. How to Validate file before uploading

How to upload files to server

First we have to create a view that contains form fields and a field with input type file.
Lets call is as Register.php

 <?php echo form_open_multipart('welcome/register/'); ?>

form_open_multipart is used to upload files. It supports the input type file. Next we have to add other form fields and fiel field.

<?php echo validation_errors('<p class="form_error">','</p>'); ?>
<?php echo form_open_multipart('welcome/register/'); ?>
<input type="text" name="firstname" placeholder="Enter your Firstname"/>
<input type="text" name="city" placeholder="Enter your City"/>
<input type="file" name="userimage">
<button type="submit">Create Account</button>

Now, the form is submitted to the Register method in Welcome controller.

Our Register method in Welcome contoller looks like this Welcome.php

public function register(){
     $this->form_validation->set_rules('firstname', 'First Name', 'required|trim|xss_clean');
     $this->form_validation->set_rules('city', 'City', 'required|trim|xss_clean');
     $this->form_validation->set_rules('userimage', 'Profile Image', 'callback_image_upload'); 
     if($this->form_validation->run() == TRUE){
         echo "Account Created Successfully";

We have to create a callback function for uploading image and validation. Method image_upload looks like the following.

function image_upload(){
	  if($_FILES['userimage']['size'] != 0){
		$upload_dir = './images/';
		if (!is_dir($upload_dir)) {
		$config['upload_path']   = $upload_dir;
		$config['allowed_types'] = 'gif|jpg|png|jpeg';
		$config['file_name']     = 'userimage_'.substr(md5(rand()),0,7);
		$config['overwrite']     = false;
		$config['max_size']	 = '5120';

		$this->load->library('upload', $config);
		if (!$this->upload->do_upload('userimage')){
			$this->form_validation->set_message('image_upload', $this->upload->display_errors());
			return false;
			$this->upload_data['file'] =  $this->upload->data();
			return true;
		$this->form_validation->set_message('image_upload', "No file selected");
		return false;

Explaining the callback function.

Steps Involved are

  • First we are checking if the file is submitted with the form. If the file is empty, we are setting the form validation error message as “No file selected”.
  • We are creating a directory if the directory does not exist.
  • Next, we have to configure the directory path, allowed upload files, filename, maximum file size, maximum width, maximum height etc.,
  • Then we are uploading the file. If upload fails, error message is set to the form validation.

Hope this is helpful.

Thanks for reading.

Feedback are welcome. Please feel free to ask questions.

Don’t forget to subscribe to Geeks.Gallery, to get free emails to your inbox.

Subscribe to Geeks.Gallery

Follow me


Web Developer at Energy Alternatives India
I'm interested in developing Web applications andkeen on learning new technologies.

View Nagarajan's Profile
Follow me

Latest posts by Nagarajan (see all)

  1. XtheWiz
  2. Sam

Leave a Reply

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