June 28, 2015

June 28, 2015
In this article, we are going to discuss about How to do the file upload and validation in CodeIgniter. I would like to explain how to upload files to server in CodeIgniter and to validate it with Form Validation library.

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

Step 1 :

Create a view that contains form fields and a field with input type file (Register.php) and add the below code in that file.

<?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>
</form>

Step 2:

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->load->library('form_validation');
     $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";
     }
     $this->load->view('register');
}

Step 3:

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)) {
             mkdir($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;
        }  
        else{
            $this->upload_data['file'] =  $this->upload->data();
            return true;
        }  
    }  
    else{
        $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.
  • 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.

0 comments:

Post a Comment