Monday 24 September 2012

Uploading multiple files in codeigniter

In this page Iam describing about uploading multiple images in codeigniter:-



public function uploadfiles(){
$this->load->helper('form');
$this->load->helper('url');
$data['urls']=base_url();
$this->load->view('templates/headermain',$data);
$this->load->view('admin/uploadfiles',$data);
$this->load->view('templates/footermain',$data);
$save=$this->input->post('saveForm');
if($save){
$error=0;
 $this->load->library('upload');  // NOTE: always load the library outside the loop

 /*Because here we are adding the "$_FILES['userfile']['name']" which increases the count, and for next loop it raises an exception, And also If we have different types of fileuploads */
 for($i=0;$i<count($_FILES);$i++)///loop through count
 {

   $_FILES['userfile']['name']    = $_FILES['filename'.$i]['name'];
   $_FILES['userfile']['type']    = $_FILES['filename'.$i]['type'];
   $_FILES['userfile']['tmp_name'] = $_FILES['filename'.$i]['tmp_name'];
   $_FILES['userfile']['error']       = $_FILES['filename'.$i]['error'];
   $_FILES['userfile']['size']    = $_FILES['filename'.$i]['size'];

   $config['file_name']     = 'test_'.$i;
   $config['upload_path'] = 'uploads/'; 
   $config['allowed_types'] = 'jpg|jpeg|gif|png';
$config['max_size'] = '1000'; 
$config['max_width'] = '1920'; 
$config['max_height'] = '1280';
   $config['overwrite']     = FALSE;

  $this->upload->initialize($config);

  if($this->upload->do_upload())
  {
    $error += 0;
  }else{
    $error += 1;
  }
 }

 if($error > 0){ echo "error"; }else{ echo "success"; }
}
}


No comments:

Post a Comment