title: Codeigniter - How to add Images to my CRUD methods?
body: <p>I am working on my second CI application. I have created some simple CRUD methods in my controller. My client has requested that each record includes an image. I have searched the forums and other resources for help but haven’t had much luck.</p>
  
  <p>I have managed to upload files into a directory using the File Uploading Class, my problem though is how to associate the uploaded file/s with the relevant record.</p>
  
  <p>Here are the relevant parts of my MVC.., any help/point in the right direction would be appreciated.</p>
  
  <p>View - admin/locationEdit.php</p>
  
  <pre><code>&lt;form method="post" action="&lt;?php echo $action; ?&gt;"&gt;
          &lt;div class="data"&gt;
          &lt;table&gt;
              &lt;tr&gt;
                  &lt;td width="30%"&gt;ID&lt;/td&gt;
                  &lt;td&gt;&lt;input type="text" name="id" disabled="disable" class="text" value="&lt;?php echo $this-&gt;validation-&gt;id; ?&gt;"/&gt;&lt;/td&gt;
                  &lt;input type="hidden" name="id" value="&lt;?php echo $this-&gt;validation-&gt;id; ?&gt;"/&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td valign="top"&gt;Name&lt;span style="color:red;"&gt;*&lt;/span&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input type="text" name="name" class="text" value="&lt;?php echo $this-&gt;validation-&gt;name; ?&gt;"/&gt;
                  &lt;?php echo $this-&gt;validation-&gt;name_error; ?&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td valign="top"&gt;Address&lt;span style="color:red;"&gt;*&lt;/span&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input type="text" name="address" class="text" value="&lt;?php echo $this-&gt;validation-&gt;address; ?&gt;"/&gt;
                      &lt;?php echo $this-&gt;validation-&gt;address_error; ?&gt;&lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;
                  &lt;td&gt;&amp;nbsp;&lt;/td&gt;
                  &lt;td&gt;&lt;input type="submit" value="Save"/&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
          &lt;/div&gt;
          &lt;/form&gt; 
  </code></pre>
  
  <p>Controller - location.php</p>
  
  <pre><code>function add(){
          // set validation properties
          $this-&gt;_set_fields();
  
          // set common properties
          $data['title'] = 'Add new location';
          $data['message'] = '';
          $data['action'] = site_url('admin/location/addLocation');
          $data['link_back'] = anchor('admin/location/index/','Back to list of locations',array('class'=&gt;'back'));
  
  
  
              // Write to $title
        $this-&gt;template-&gt;write('title', 'Admin - Add New Location!');
      // Write to $sidebar
        $this-&gt;template-&gt;write_view('content', 'admin/locationEdit', $data);
        // Render the template
        $this-&gt;template-&gt;render();
  
      }
  
  function addLocation(){
          // set common properties
          $data['title'] = 'Add new location';
          $data['action'] = site_url('admin/location/addLocation');
          $data['link_back'] = anchor('admin/location/index/','Back to list of locations',array('class'=&gt;'back'));
  
          // set validation properties
          $this-&gt;_set_fields();
          $this-&gt;_set_rules();
  
          $config['allowed_types'] = 'gif|jpg|png';
          $config['max_size'] = '100';
          $config['max_width']  = '1024';
          $config['max_height']  = '768';
  
           $path_to_uploads='./uploads';
            $config['upload_path'] = $path_to_uploads;
            $this-&gt;load-&gt;library('upload', $config);
  
              $upload_data=$this-&gt;upload-&gt;data();
              $file_name=$upload_data['file_name'];
              $full_file_path = $path_to_uploads.'/'.$file_name;
  
          // run validation
          if ($this-&gt;validation-&gt;run() == FALSE){
              $data['message'] = '';
          }else{
  
  
             // save data
              $location = array('name' =&gt; $this-&gt;input-&gt;post('name'),
                              'address' =&gt; $this-&gt;input-&gt;post('address'),
                              'image_url' =&gt; $full_file_path);
              $id = $this-&gt;locationModel-&gt;save($location);
  
              // set form input name="id"
              $this-&gt;validation-&gt;id = $id;
  
              // set user message
              $data['message'] = '&lt;div class="success"&gt;add new location success&lt;/div&gt;';
          }
  
          // Write to $title
        $this-&gt;template-&gt;write('title', 'Admin - Add New Location!');
      // Write to $sidebar
        $this-&gt;template-&gt;write_view('content', 'admin/locationEdit', $data);
        // Render the template
        $this-&gt;template-&gt;render();
      }
  </code></pre>