0

I want to upload files with CodeIgniter through generated numbered upload fields :

<input type='file' name='field[0]' />
<input type='file' name='field[1]' />

These upload fields are among another inputs fields :

<input type='file' name='field[0]' />
<input type='TEXT' name='field[1]' />
<input type='file' name='field[2]' />

I just want to upload the files when the user click on the Submit button and keep this specific inputs structure

I've tried this but it doesn't work :

     for($i=0; $i<count($_FILES); $i++)
     {

       $config['upload_path']   = './uploads/';
       $config['allowed_types'] = 'jpg|jpeg|gif|png';
       $config['overwrite']     = FALSE;

      $this->upload->initialize($config);
$this->upload->do_upload('field['.$i.']');


     }

Any help would be very appreciated! Thanks!

user990463
  • 439
  • 1
  • 7
  • 27
  • You should take a look at the PHP Manual about uploading multiple files, the array structure is different to post/get elements, especially when you've got an array of files: http://php.net/manual/en/features.file-upload.multiple.php – hakre Oct 17 '11 at 23:59
  • Yeah I can retrieve the $_FILES['field']['name'] but I can't figure it out to upload the files with CodeIgniter way – user990463 Oct 18 '11 at 07:31
  • For codeigniter, please see [File Uploading Class](http://codeigniter.com/user_guide/libraries/file_uploading.html) (works once *per* file), [codeigniter multiple file upload](http://stackoverflow.com/questions/1908247/codeigniter-multiple-file-upload) and [Upload Multiple Files with Codeigniter](http://darrenonthe.net/2011/05/08/upload-multiple-files-with-codeigniter/) – hakre Oct 18 '11 at 09:45

1 Answers1

1

That did the trick :

foreach ($_FILES['field']['name'] as $i => $name) { 

     if ($_FILES['field']['error'][$i] == 4) { 
         continue;  
     } 

     if ($_FILES['field']['error'][$i] == 0) { 

          if ($_FILES['field']['size'][$i] > 99439443) { 
             $message[] = "$name exceeded file limit."; 
             continue;   
          } 

         /* 
          * Check File Extension 
          * Move  File            
          */   

            echo $name;
            echo '<br />';

            $uploads_dir = $config['upload_path'] = './uploads/thumbs/'.$this->session->userdata('raw_name');

            $tmp_name = $_FILES["field"]["tmp_name"][$i];
            $name = $_FILES["field"]["name"][$i];
            move_uploaded_file($tmp_name, "$uploads_dir/$name");        

     } 
}

Unfortunately, not in CodeIgniter way :( It doesn't work and I don't know why, but this works well

user990463
  • 439
  • 1
  • 7
  • 27