title: how to create form for multple image upload in codeigniter
body: <p>i have created form for multiple image upload.but not working that form.only one image uploading..i want multiple images are to be upload to folder and save image name in database..</p>
  
  <p>My View File </p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Product Upload&lt;/title&gt;
  &lt;style&gt;
  #container
  {
      width:750px;
      margin:0 auto;
      //border:5px solid #000000;
  }
  #container form input[type="text"] {
      height:30px; 
  }
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="container" align="center"&gt;
  &lt;form name="product" action="&lt;?php echo base_url;?&gt;admin/login/upload" method="POST" enctype="multipart/form-data" class="form-horizontal"&gt;
  &lt;table&gt;
  &lt;h3&gt;Add New Product&lt;/h3&gt;
  &lt;tr&gt;&lt;td&gt;Categories&lt;/td&gt;&lt;td&gt;&lt;select name="catid"&gt;&lt;option&gt;Categories&lt;/option&gt;
  &lt;?php if(isset($category_details))
  {foreach($category_details as $keys=&gt;$values){ ?&gt;
  &lt;option value="&lt;?php echo $values['cat_id'];?&gt;"&gt;&lt;?php echo $values['cat_name'];?&gt;&lt;/option&gt;
  &lt;?php }
  }?&gt;
  &lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;
      &lt;td&gt;Product Name:&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="pname"&gt;&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;&lt;td&gt;&lt;input type="file" multiple="true" name="userfile" size="20" /&gt;&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;&lt;td&gt;&lt;br&gt;Product Image:&lt;/td&gt;&lt;td&gt;&lt;br&gt;&lt;input type="file" name="pimage[]" id="pimage" multiple&gt;&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;&lt;td&gt;&lt;br&gt;Description:&lt;/td&gt;&lt;td&gt;&lt;br&gt;&lt;textarea name="pdescription"&gt;&lt;/textarea&gt;&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;&lt;td&gt;&lt;br&gt;Price:&lt;/td&gt;&lt;td&gt;&lt;br&gt;&lt;input type="text" name="price"&gt;&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;&lt;td colspan="2" align="center"&gt;&lt;br&gt;&lt;input type="submit" name="submit" value="ADD" class="btn btn-primary"&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  &lt;/form&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My Controller File</p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
  class Login extends CI_Controller {
  
      /**
       * Index Page for this controller.
       *
       * Maps to the following URL
       *      http://example.com/index.php/welcome
       *  - or -  
       *      http://example.com/index.php/welcome/index
       *  - or -
       * Since this controller is set as the default controller in 
       * config/routes.php, it's displayed at http://example.com/
       *
       * So any other public methods not prefixed with an underscore will
       * map to /index.php/welcome/&lt;method_name&gt;
       * @see http://codeigniter.com/user_guide/general/urls.html
       */
  
      public function __construct()
       {
          parent::__construct();
         $this-&gt;load-&gt;helper(array('form', 'url'));
  
          $this-&gt;load-&gt;library('form_validation');
         $this-&gt;load-&gt;model('admin/category_model');
         $this-&gt;load-&gt;model('admin/loginauth_model');
         $this-&gt;load-&gt;model('login_model');
         $this-&gt;load-&gt;library('session');
          $this-&gt;load-&gt;helper('url');
          $this-&gt;load-&gt;helper('cookie');
          $this-&gt;load-&gt;library('encrypt');
         $this-&gt;load-&gt;library('image_CRUD');
       }
  
      public function index()
      {
  
          $this-&gt;load-&gt;view('admin/login');
  
      }
      public function loadproduct()
      {
          $category_details=$this-&gt;category_model-&gt;getCategoryDetails();
          $outputdata["category_details"]=$category_details;
          $this-&gt;load-&gt;view('admin/product',$outputdata); 
     }
  
   public function loginAuth()
   {
          $this-&gt;form_validation-&gt;set_rules('email','Enter Email','required|trim|max_length[50]|xss_clean');
      $this-&gt;form_validation-&gt;set_rules('password','Enter your Password','required|trim|max_length[50]|xss_clean');
  
          if ($this-&gt;form_validation-&gt;run() == FALSE)
              {
                  $this-&gt;load-&gt;view('admin/login');
              }
             else
              {
                  $username=$_POST['email'];  
              $password=$_POST['password'];
  
                 $user_details=$this-&gt;loginauth_model-&gt;logincheck($username,$password);
                 //print_r($checkauth);
                 if($user_details)
                   {
                      if($this-&gt;session-&gt;userdata('adminusername'))
                      {
                          $adminusername=$this-&gt;session-&gt;userdata('adminusername');
                          $outputdata['username']=$adminusername;
                      }
                         $category_details=$this-&gt;category_model-&gt;getCategoryDetails();
  
                          $outputdata['category_details']=$category_details;
                      $this-&gt;load-&gt;view('admin/dashboard',$outputdata);
              }
             }
           }
  
      public function category()
      {
          //$this-&gt;load-&gt;view('admin/category');
          $this-&gt;form_validation-&gt;set_rules('cat_name','category name','required|trim|max_length[50]|xss_clean');
        $this-&gt;form_validation-&gt;set_rules('cat_desc','category description','required|trim|max_length[50]|xss_clean');
  
          if ($this-&gt;form_validation-&gt;run() == FALSE)
              {
                  $this-&gt;load-&gt;view('admin/category');
              }
             else
              {
                 $addcategory=$this-&gt;category_model-&gt;addcategory($_POST);
                 if($addcategory)
               {
  
                 //**************************pending
               $category_details=$this-&gt;category_model-&gt;getCategoryDetails();
  
                 $outputdata['category_details']=$category_details;
                //print_r($outputdata);
              $this-&gt;load-&gt;view('admin/categorylist',$outputdata);
               }
          }
      }
      public function categorylist()
      {
                      //echo image_url;
         $category_details=$this-&gt;category_model-&gt;getCategoryDetails();
  
          $outputdata['category_details']=$category_details;
          $outputdata['image_url']=image_url;
  
          $this-&gt;load-&gt;view('admin/categorylist',$outputdata);
          $this-&gt;load-&gt;view('admin/category');
  
   }
  public function userdetails()
      {
          $user_details=$this-&gt;login_model-&gt;userdetails();
  
          $outputdata['user_details']=$user_details;
  
          $this-&gt;load-&gt;view('admin/userdetails',$outputdata);     
  
      }
   public function upload()
          {
              $productname=$_POST["pname"];
          $description=$_POST["pdescription"];
              $price=$_POST["price"];
              $catid=$_POST["catid"];
          $name_array = array();
       echo $count = count($_FILES['pimage']['size']);
          foreach($_FILES as $key=&gt;$value)
          for($s=0; $s&lt;=$count-1; $s++) {
          $_FILES['pimage']['name']=$value['name'][$s];
          $_FILES['pimage']['type']    = $value['type'][$s];
          $_FILES['pimage']['tmp_name'] = $value['tmp_name'][$s];
          $_FILES['pimage']['error']       = $value['error'][$s];
          $_FILES['pimage']['size']    = $value['size'][$s];   
              $config['upload_path'] = FCPATH.'img/product_uploads/original/';
              $config['allowed_types'] = 'gif|jpg|png|jpeg|GIF|PNG|JPG|JPEG';
              $config['max_size'] = '100';
              $config['max_width']  = '150';
              $config['max_height']  = '180';
          $this-&gt;load-&gt;library('upload', $config);
          $this-&gt;upload-&gt;do_upload();
          $data = $this-&gt;upload-&gt;data();
          $name_array[] = $data['file_name'];
                  //$products=$this-&gt;category_model-&gt;addproduct($catid,$productname,$description,$imagename,$imagesize,$price,$path);
              }
              $names= implode(',', $name_array);
  /*          $this-&gt;load-&gt;database();
              $db_data = array('id'=&gt; NULL,
                               'name'=&gt; $names);
          $this-&gt;db-&gt;insert('testtable',$db_data);
  */          print_r($names);
              //echo FCPATH;
              //$productname=$_POST["pname"];
          //  $description=$_POST["pdescription"];
          //  $price=$_POST["price"];
              //$catid=$_POST["catid"];
  
          //   $path = FCPATH.'img/product_uploads/original/';                        
          //  $valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP","PNG");  
          //  if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {  
          //  $imagename = $_FILES['pimage']['name'];  
      //      $imagesize = $_FILES['pimage']['size'];  
  
          //  list($txt, $ext) = explode(".", $imagename);  
              //$products=$this-&gt;category_model-&gt;addproduct($catid,$productname,$description,$imagename,$imagesize,$price,$path);
                //print_r($products);
      //      $tmp = $_FILES['pimage']['tmp_name'];  
  //if(move_uploaded_file($tmp, $path.$imagename)) {  
          //  $product_details=$this-&gt;category_model-&gt;getProductDetails();
              //print_r($product_details);
              //if(isset($products)){
                  //echo "aa";
          //    $this-&gt;loadproduct();
               //}
          //  }  
              //else  
          //  {
              //echo "Image Upload Failed.";  
      //}
      }
  public function logout()
      {
          $newdata = array(
          'adminuser_id'   =&gt;'',
          'adminusername'  =&gt;'',
          'adminemail'     =&gt; '',
          'logged_in' =&gt; FALSE,
          );
          $this-&gt;session-&gt;unset_userdata($newdata);
          $this-&gt;session-&gt;sess_destroy();
          $this-&gt;index();
      }
  
  }
  
  
  
  
  
  /* End of file welcome.php */
  /* Location: ./application/controllers/welcome.php */
  </code></pre>