title: Registration form with image upload in Codeigniter
body: <p>I have a user registration form and it's working fine but would like to add an image upload feature inside it. This is basically what I'd like to achieve</p>
  
  <p>ScreenShot 1<img src="https://i.stack.imgur.com/ue5rd.png" alt="Screenshot 1"></p>
  
  <p>ScreenShot 2<img src="https://i.stack.imgur.com/yGVQz.png" alt="screenshot 2"></p>
  
  <p>So far, this is what I have done</p>
  
  <p><strong>Controller:</strong> </p>
  
  <pre><code>class Employees extends CI_Controller {
  var $pgToLoad;
  
  public function __construct() {
      parent::__construct();
      #this will start the session
      session_start();
      $this-&gt;load-&gt;helper(array('form', 'url'));
  
  
      if(!isset($_SESSION['userId']) || !isset($_SESSION['userLevel']) || !isset($_SESSION['employeeid']) || !isset($_SESSION['firstname']) || !isset($_SESSION['lastname'])) {
          redirect('home', 'location');
      }
  
      #this will load the model
      $this-&gt;load-&gt;model('Contents');
  
      #get last uri segment to determine which content to load
      $continue = true;
      $i = 0;
      do {
          $i++;
          if ($this-&gt;uri-&gt;segment($i) != "") $this-&gt;pgToLoad = $this-&gt;uri-&gt;segment($i);
          else $continue = false;             
      } while ($continue);        
  }
  
  public function index() {       
      $this-&gt;main();
  }   
  
  public function main() {
      #set default content to load 
      $this-&gt;pgToLoad = empty($this-&gt;pgToLoad) ? "employees" : $this-&gt;pgToLoad;
      $disMsg = "";
  
      #this will delete the record selected
      if($this-&gt;uri-&gt;segment(2) == 'delete') { 
          $this-&gt;deleteRecord();
      }
  
      #this will check if the post value is trigger
      if(isset($_POST['addnew'])) {
          $this-&gt;addRecord(); 
      }                   
  
      #this will check if the post value is trigger
      if(isset($_POST['saveinfo'])) {
          $this-&gt;updateinfo();    
      }           
  
      if($this-&gt;uri-&gt;segment(2) == 'add' || $this-&gt;uri-&gt;segment(2) == 'edit') {
          #this display the form for products
          $this-&gt;displayForm();
      } else {
          #this will display the job orders
          $this-&gt;getAllEmployees();       
      }   
  
  
      if($this-&gt;uri-&gt;segment(2) == 'print') {
          #this display the form for products
          $this-&gt;pdf();
      } 
  
  if($this-&gt;uri-&gt;segment(2) == 'do_upload') {
          #this display the form for products
          $this-&gt;do_upload();
      } 
  
      #this will logout the user and redirect to the page
      if($this-&gt;uri-&gt;segment(2) == 'logout') {
          session_destroy();
          redirect('home', 'location');
      }                   
  
      $data = array ( 'pageTitle' =&gt; 'Payroll System | ADMINISTRATION',
                      'disMsg'    =&gt; $disMsg,                                             
                      'mainCont'  =&gt; $this-&gt;mainCont );
  
      $this-&gt;load-&gt;view('mainTpl', $data, FALSE);
  }
  
  
  function do_upload(){
  
      if($this-&gt;input-&gt;post('upload')){
  
          $config['upload_path'] = './uploads/';
          $config['allowed_types'] = 'gif|jpg|png';
          $config['max_size']    = '1024';
          $config['max_width']  = '1024';
          $config['max_height']  = '768';
          $this-&gt;load-&gt;library('upload', $config);
  
          if ( ! $this-&gt;upload-&gt;do_upload()){
  
          $error = array('error' =&gt; $this-&gt;upload-&gt;display_errors());
          $this-&gt;load-&gt;view('pages/employeesform', $error);
          }
  
          else{
  
          $data=$this-&gt;upload-&gt;data();
          $this-&gt;thumb($data);
          $file=array(
          'img_name'=&gt;$data['raw_name'],
          'thumb_name'=&gt;$data['raw_name'].'_thumb',
          'ext'=&gt;$data['file_ext'],
          'upload_date'=&gt;time()
          );
          $this-&gt;Contents-&gt;add_image($file);
          $data = array('upload_data' =&gt; $this-&gt;upload-&gt;data());
          $this-&gt;load-&gt;view('pages/upload_success', $data);
          }
      }
  
      else{
  
      redirect(site_url('employees'));
  
      }
  
  }
  
  function thumb($data){
  
      $config['image_library'] = 'gd2';
      $config['source_image'] =$data['full_path'];
      $config['create_thumb'] = TRUE;
      $config['maintain_ratio'] = TRUE;
      $config['width'] = 275;
      $config['height'] = 250;
      $this-&gt;load-&gt;library('image_lib', $config);
      $this-&gt;image_lib-&gt;resize();
  
  }
      public function displayForm() {
      $data['level'] = $this-&gt;Contents-&gt;exeGetUserLevel();
      $data['status'] = $this-&gt;Contents-&gt;exeGetUserStatus();
  
      $data['employee'] = $this-&gt;Contents-&gt;exeGetEmpToEdit($_SESSION['userId']);      
      if($this-&gt;uri-&gt;segment(2) == 'edit') { 
          $data['employee'] = $this-&gt;Contents-&gt;exeGetEmpToEdit($_SESSION['userId']);
          $data['emp'] = $this-&gt;Contents-&gt;exeGetEmpToEdit($this-&gt;uri-&gt;segment(3));
          $data['info'] = $this-&gt;Contents-&gt;exeGetUserInfo($this-&gt;uri-&gt;segment(3));    
          $this-&gt;mainCont = $this-&gt;load-&gt;view('pages/employeesform', $data, TRUE);
      }                   
      $this-&gt;mainCont = $this-&gt;load-&gt;view('pages/employeesform', $data, TRUE);                
  }
  
  #this will add new record
  public function addRecord() {
      if(empty($_POST['fname']) || empty($_POST['mname']) || empty($_POST['lname']) || empty($_POST['empass']) || empty($_POST['emailadd']) || empty($_POST['gender']) || empty($_POST['datehired']) || empty($_POST['salary'])) {    
          $disMsg = "Please fill up the form completely.";
          $_SESSION['disMsg'] = $disMsg;  
      } else {
          $addNew = $this-&gt;Contents-&gt;exeAddNewRecord();
  
          if($addNew['affRows'] &gt; 0) {
              $_SESSION['disMsg'] = "New Employee has been added.";
              redirect('employees', 'location');          
          } else {
              $disMsg = "Unable to add new employee.";
          }       
      }           
  }
  </code></pre>
  
  <p><strong>Views:</strong></p>
  
  <pre><code> &lt;?php echo form_open_multipart('employees/do_upload');?&gt;
  
              &lt;img id="preview" style = "width: 200px; height: 200px;"&gt;
              &lt;input type="file" name = "userfile" id="input"&gt;
  
              &lt;br /&gt;&lt;br /&gt;
  
              &lt;input type="submit" value="upload" name="upload" /&gt;
  
              &lt;/form&gt;
  
  
  &lt;form action="" method="post" enctype="multipart/form-data" id="empform" role="form" name="empform"&gt;
              &lt;div class="box-body"&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="col-lg-6"&gt;
                          &lt;div class="input-field col s12"&gt;
                              &lt;label&gt;Firstname&lt;/label&gt;
                              &lt;input type="text" id="fname" name="fname" class="form-control" value="&lt;?php if(!empty($_POST['fname'])) { echo $_POST['fname']; } elseif(!empty($emp[0]['firstname'])) { echo $emp[0]['firstname']; } ?&gt;"&gt;
                          &lt;/div&gt;
                          &lt;div class="input-field col s12"&gt;
                              &lt;label&gt;Middle Name&lt;/label&gt;
                              &lt;input type="text" id="mname" name="mname" class="form-control" value="&lt;?php if(!empty($_POST['mname'])) { echo $_POST['mname']; } elseif(!empty($emp[0]['middlename'])) { echo $emp[0]['middlename']; } ?&gt;"&gt;
                          &lt;/div&gt;
                          &lt;div class="input-field col s12"&gt;
                              &lt;label&gt;Password&lt;/label&gt;
                              &lt;input type="password" id="empass" name="empass" class="form-control" value="&lt;?php if(!empty($_POST['empass'])) { echo $_POST['empass']; } ?&gt;"&gt;
                          &lt;/div&gt;
                          &lt;div class="input-field col s12"&gt;
                              &lt;label&gt;Employee ID&lt;/label&gt;
                              &lt;input type="text" id="empno" name="empno" class="form-control" value="&lt;?php if(!empty($_POST['empno'])) { echo $_POST['empno']; } elseif(!empty($emp[0]['employeeid'])) { echo $emp[0]['employeeid']; } ?&gt;"&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                          &lt;div class="col-lg-4" style="padding-left:0;"&gt;
                              &lt;?php if($this-&gt;uri-&gt;segment(2) == "edit") { ?&gt;
                              &lt;button type="submit" name="saveinfo" class="btn btn-lg btn-primary btn-block"&gt;Save&lt;/button&gt;
                              &lt;?php } else { ?&gt;
                              &lt;button type="submit" name="addnew" class="btn btn-lg btn-primary btn-block"&gt;Save&lt;/button&gt;
                              &lt;?php } ?&gt;
  
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
  </code></pre>
  
  <p>As you can see in views, there are 2 forms. First form is for image upload and the other one is user registration form. How will I be able to include the image upload in the reg form and perform image upload, save user info and form validation once save button is clicked?</p>