CodeIgniter Forums

Full Version: how to upload multiple file in a single form in codeigniter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
can anybody help me out to upload multiple file from a single form. if possible please give some example. below is my code in model for upload function it works fine for single upload, but doesn't work for multiple uploads from a single form.

Code:
function upload(){
         $config[$a]=array(
            'allowed_types'=>'xlsx',
            'upload_path'=>  $this->gallery_path,
            'overwrite'=>true,
            'max_size'=>2000,
        );
         $this->load->library('upload',$config);
        $this->upload->do_upload();
        
    }

Thanks in Advance.

El Forum

[eluser]TheFuzzy0ne[/eluser]
It's not possible to do that natively. People seem to be using this: https://github.com/stvnthomas/CodeIgniter-Multi-Upload

El Forum

[eluser]RaGe10940[/eluser]
Had the same problem as you did solved it on this thread.

Its long but the answers are here.

Any questions just post on this thread and Fuzzy or myself will help you Big Grin

El Forum

[eluser]Bigil Michael[/eluser]
use for loop to upload more than one image at a time

controller

Code:
function demo(){
  
  if($this->input->post('Submit')){
     for($i=1;$i<=6;$i++)
   {
      if($_FILES['photo_'.$i]['name']!=''){
       //upload thumbnail      $c
     $config['upload_path'] = 'Your folder path';
     $config['allowed_types'] = 'gif|jpg|jpeg|png';
     $config['file_name'] = '0'.$i;
     $config['max_size'] = '150';    
     $this->load->library('upload');
     $this->upload->initialize($config);
     if ( ! $this->upload->do_upload('photo_'.$i))
     {
      $data['alert'] = $this->upload->display_errors();
      
     }
     else
     {
      $upload_data = $this->upload->data();
      $filename = $upload_data['file_name'];
      $width = $upload_data['image_width'];
      $height = $upload_data['image_height'];
      $config1 = array();
      $this->load->library('image_lib');
      $config1['source_image'] = 'your folder/'.$filename;
      if($width>762){
       //resize image
       $sizes = array('width' => $width, 'height' => $height, 'new_width' => 762, 'new_height' => 325);
       $newsize = array();
       $newsize = $this->image_lib->size_calculator($sizes);
       $config1['maintain_ratio'] = FALSE;
       $config1['width'] = $newsize['new_width'];
       $config1['height'] = $newsize['new_height'];
       $this->image_lib->initialize($config1);
       $this->image_lib->resize();
       $this->image_lib->clear();
      }
                                                // creating thumb
      $config1['maintain_ratio'] = FALSE;
      $config1['width'] = 762;
      $config1['height'] = 325;
      $config1['create_thumb'] = TRUE;
      $config1['thumb_marker'] = 'thumb_';
      $this->image_lib->initialize($config1);
      $this->image_lib->resize();      
      $this->Your model->save_image('photo_'.$i,$filename);
      $data['alert'] = 'image uploaded';
     }
      }
    }
    $data['image'] = $this->Yoourmodel->get_homepage(); // select photos
    $this->load->view('your view', $data);
      
  }else{
    $this->load->view('your view', $data);
  }
}

model

Code:
function save_image($field,$filename=''){
  
    if($filename!=''){
   $data[$field] = $filename;
  }
  $this->db->where('id',1);// item  id
  $this->db->update('home',$data);
}

El Forum

[eluser]TheFuzzy0ne[/eluser]
I don't think that's what the OP means. I suspect he has an upload field with the multiple="" attribute, meaning you can select any number of files from the file picker window, and they are uploaded in an array.

El Forum

[eluser]RaGe10940[/eluser]
Bigil you need to have a look at this extended library :

https://github.com/stvnthomas/CodeIgniter-Multi-Upload


you are doing way to much work to upload multiple files.

Literally with this library all you have to do is :

Code:
name="file[]"

$this->upload->do_multi_upload('file');

and your done.

El Forum

[eluser]Bigil Michael[/eluser]
Good one. Thanks RaGe10940.

El Forum

[eluser]RaGe10940[/eluser]
Heres a more in depth look at some of my code to further assist any one else looking into doing multiple uploads with a single form field.

as Fuzzy said with one form field just use

Code:
multiple=""

then do :
Code:
$this->load->library('upload'); // Load Library

   $this->upload->initialize(array( // takes an array of initialization options
       "upload_path" => "/usr/local/var/www/Test/ci/uploads/",
       "overwrite" => TRUE,
       "encrypt_name" => TRUE,
       "remove_spaces" => TRUE,
       "allowed_types" => "txt|pdf",
       "max_size" => 300,
       "xss_clean" => FALSE
   )); // These are just my options. Also keep in mind with PDF's YOU MUST TURN OFF xss_clean

   if ($this->upload->do_multi_upload("files")) { // use same as you did in the input field
       $return = $this->upload->get_multi_upload_data(); // then just use function from the extended library

in my case I had to do this for a email system which Fuzzy helped me on so then do this :

Code:
foreach ($return as $file) { // loop over the upload data
    $this->email->attach($file['full_path']); // attach the full path as an email attachments :D
       }

enjoy.

El Forum

[eluser]Tiến Thành[/eluser]
[quote author="TheFuzzy0ne" date="1365534088"]It's not possible to do that natively. People seem to be using this: https://github.com/stvnthomas/CodeIgniter-Multi-Upload[/quote]
Thank you so much

El Forum

[eluser]www.sblog.in[/eluser]
Example http://www.webtuts.in/codeigniter-multip...es-upload/