title: how to store and retrieve images in database using CodeIgniter
body: <p>I am using a WAMP server, and I want to upload images in the database using CI.The image variable in database is of blob datatype. My question's as follows:</p>
  
  <p>1) How to store the image instead of the file name and what datatypes should I use?</p>
  
  <p>2) How to retrieve images from the DB?</p>
  
  <p>My controller's code:</p>
  
  <pre><code>&lt;?php class Image_control extends CI_Controller{
  function index()
  {
      //$this-&gt;load-&gt;view('image_view');
  
      //$this-&gt;Image_model-&gt;do_upload();
  
      $data['images']=$this-&gt;Image_model-&gt;get_images();
      $this-&gt;load-&gt;view('image_view',$data);  
  }
  function do_upload()
  {
      $config = array(
          'allowed_types' =&gt; 'jpg|png|bmp', 
          'upload_path'=&gt;'./images1/',
          'max_size'=&gt;2000
      );
      $this-&gt;load-&gt;library('upload',$config);
      if (!$this-&gt;upload-&gt;do_upload()) {
          $errors[]=array('error'=&gt;$this-&gt;upload-&gt;display_errors());
          $this-&gt;load-&gt;view('image_view',$errors);
      }
      $image_path=$this-&gt;upload-&gt;data();
      $file_name=$image_path['file_name'];
      $config = array(
          'a_name' =&gt; $this-&gt;input-&gt;post('a_name'),
          'a_details'=&gt;$this-&gt;input-&gt;post('a_info'),
          'a_photo'=&gt;$file_name
      );
      $insert=$this-&gt;db-&gt;insert('animalstore',$config);
      return $insert;
  }   
  }
  ?&gt;
  </code></pre>
  
  <p>My model's code:</p>
  
  <pre><code>&lt;?php class Image_model extends CI_Model {
  function get_images()
  {
      $query = $this-&gt;db-&gt;get('animalstore');
      if($query-&gt;num_rows &gt; 0 )
      {
          foreach($query-&gt;result() as $rows)
          {
              $data[] = $rows;
          }
          return $data;
      }
  }
  }
  ?&gt;
  </code></pre>
  
  <p>And finally here's the code for my view:</p>
  
  <p>
  </p>
  
  <pre><code>&lt;?php
      echo form_open_multipart('image_control/do_upload');
      echo form_input('a_name','Animal Name');
      echo form_input('a_info','Animal Information');
      echo form_upload('userfile');
      echo form_submit('upload','Upload');
      echo form_close();
  ?&gt;
  
  &lt;?php foreach ($images as $image):?&gt;
  &lt;h1&gt;&lt;?php echo $image-&gt;a_name;?&gt;&lt;/h1&gt;
  &lt;h1&gt;&lt;?php echo $image-&gt;a_details;?&gt;&lt;/h1&gt;
  &lt;img src = "/&lt;?php// echo ltrim($image-&gt;a_photo, '/'); ?&gt;" &gt;
  &lt;img src="http://localhost/ci_test/images1/&lt;?php echo $image-&gt;a_photo;?&gt;"/&gt; 
  &lt;img src="&lt;?php //echo sprintf("images/%s", $image['screenshot']);?&gt;" /&gt;
  &lt;h1&gt;&lt;?php// echo $image-&gt;a_photo;?&gt;&lt;/h1&gt;
  &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>I tried solving it in different ways and searched for my problem but I didn't find any appropriate answer.</p>