title: Image uploading using codeigniter file uploading class
body: <p>This is not a how to upload image question . I have almost successfully managed to add a image upload function into my add client function . It works well when i try to upload a valid file .. but when i select a invalid file or larger file then it shows undefined variable <code>upload_data</code> and codeigniter database error where <code>img_path</code> is <code>NULL</code> it says <code>Column 'img_path' cannot be null</code>. why this function isn't working <code>$this-&gt;upload-&gt;display_errors();</code> . The validation errors are showing nicely but no file validation error showing up.</p>
  
  <p>I am using Codeigniter and hmvc</p>
  
  <p>here is my controller</p>
  
  <pre><code>&lt;?php
  
  class Clients extends MX_Controller{
  
      function __construct(){
          parent::__construct();
          $this-&gt;load-&gt;model('mdl_clients');
      }
  
      function add(){
          $data['success'] = null;
          $data['errors']= null;
          if($_POST){
              $config_arr = array(
                  'upload_path'   =&gt; './uploads/',
                  'allowed_types' =&gt; 'gif|jpg|png',
                  'max_size'      =&gt; '2048',
                  'max_width'     =&gt; '1024',
                  'max_height'    =&gt; '768',
                  'encrypt_name'  =&gt; true,
                  );         
              $this-&gt;load-&gt;library('upload', $config_arr);
              if (!$this-&gt;upload-&gt;do_upload()) {
                  $data['errors'] = $this-&gt;upload-&gt;display_errors(); // this isn't working
              } else {
                  $upload_data = $this-&gt;upload-&gt;data();
              }
  
              $config=array(
                  array(
                      'field'=&gt;'firstName',
                      'label'=&gt;'First Name',
                      'rules'=&gt;'required|max_length[15]|min_length[3]'
                  ),
                  array(
                      'field'=&gt;'city',
                      'label'=&gt;'City',
                      'rules'=&gt;'required'
                  ),
                  array(
                      'field'=&gt;'mobile_phone',
                      'label'=&gt;'Mobile Number',
                      'rules'=&gt;'required'
                  ),
                  array(
                      'field'=&gt;'email',
                      'label'=&gt;'Email',
                      'rules'=&gt;'required|is_unique[clients.email]|valid_email'
                  ),
              );
              $this-&gt;load-&gt;library('form_validation');
              $this-&gt;form_validation-&gt;set_rules($config);
              if($this-&gt;form_validation-&gt;run() == FALSE){
                  $data['errors'] = validation_errors();
              }else{
                  $data=array(
                      'img_path'=&gt;$upload_data['file_name'],
                      'firstName'=&gt;$_POST['firstName'],
                      'email'=&gt;$_POST['email'],
                      'city'=&gt;$_POST['city'],
                      'mobile_phone'=&gt;$_POST['mobile_phone'],
                  );
                  $this-&gt;mdl_clients-&gt;add($data);
                  $data['success'] = 1;
                  $data['errors']= 0;
              }
          }
              $data['title'] = 'Add Client Database';
              $data['main_content'] = 'clients/add';
              echo Modules::run('templates/admin', $data);
  
      }
  </code></pre>
  
  <p>and my view file .. add.php</p>
  
  <pre><code>&lt;? if($success==1) {?&gt;
      &lt;div class="alert alert-success"&gt;
          &lt;a class="close" data-dismiss="alert" href="#"&gt;&amp;times;&lt;/a&gt;
          Data Has been Updated ! 
      &lt;/div&gt;
  &lt;? } ?&gt;
  &lt;?php if($errors) { ?&gt;
      &lt;div class="alert alert-error" &gt;
          &lt;a class="close" data-dismiss="alert" href="#"&gt;&amp;times;&lt;/a&gt;
          &lt;?=$errors?&gt;
      &lt;/div&gt;
  &lt;? } ?&gt;
  
  &lt;?php $attributes = array('class' =&gt; 'form-horizontal');
  echo form_open_multipart('clients/add', $attributes); ?&gt;
      &lt;fieldset&gt;
          &lt;!-- Address form --&gt;
  
  &lt;h2&gt;Client Information&lt;/h2&gt;
  &lt;hr /&gt;
  All Fields Marked with &lt;span style="color: red;"&gt;*&lt;/span&gt; is necessary .
      &lt;hr /&gt;
  
          &lt;!-- Upload input--&gt;
          &lt;div class="control-group"&gt;
              &lt;label class="control-label"&gt;Upload&lt;span style="color: red;"&gt;*&lt;/span&gt;&lt;/label&gt;
              &lt;div class="controls"&gt;
                  &lt;input name="userfile" name="userfile" type="file"
                  class="input-xlarge"&gt;
                  &lt;p class="help-block"&gt;&lt;/p&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;!-- firstName input--&gt;
          &lt;div class="control-group"&gt;
              &lt;label class="control-label"&gt;First Name&lt;span style="color: red;"&gt;*&lt;/span&gt;&lt;/label&gt;
              &lt;div class="controls"&gt;
                  &lt;input id="firstName" name="firstName" type="text" placeholder="First Name"
                  class="input-xlarge" required&gt;
                  &lt;p class="help-block"&gt;&lt;/p&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;!-- Email input--&gt;
          &lt;div class="control-group"&gt;
              &lt;label class="control-label"&gt;E-Mail&lt;span style="color: red;"&gt;*&lt;/span&gt;&lt;/label&gt;
              &lt;div class="controls"&gt;
                  &lt;input id="email" name="email" type="text" placeholder="A Valid Email Address"
                  class="input-xlarge" required&gt;
                  &lt;p class="help-block"&gt;&lt;/p&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;!-- City input--&gt;
          &lt;div class="control-group"&gt;
              &lt;label class="control-label"&gt;City&lt;span style="color: red;"&gt;*&lt;/span&gt;&lt;/label&gt;
              &lt;div class="controls"&gt;
                  &lt;input id="city" name="city" type="text" placeholder="City Name"
                  class="input-xlarge" required&gt;
                  &lt;p class="help-block"&gt;&lt;/p&gt;
              &lt;/div&gt;
          &lt;!-- Mobile input--&gt;
          &lt;div class="control-group"&gt;
              &lt;label class="control-label"&gt;Mobile Number&lt;span style="color: red;"&gt;*&lt;/span&gt;&lt;/label&gt;
              &lt;div class="controls"&gt;
                  &lt;input id="mobile_phone" name="mobile_phone" type="text" placeholder="Current Mobile Phone Number"
                  class="input-xlarge" required&gt;
                  &lt;p class="help-block"&gt;&lt;/p&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;!-- Button --&gt;
          &lt;div class="control-group"&gt;
              &lt;div class="controls"&gt;
                  &lt;button class="btn btn-success"&gt;Add to Database&lt;/button&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/fieldset&gt;
  &lt;/form&gt;
  </code></pre>