Welcome Guest, Not a member yet? Register   Sign In
How to perform the upload with multiple files
#1

I use the following code in my form .

Code:
<input type="file" name="bbs_images[]">
<input type="file" name="bbs_images[]">


I tried

PHP Code:
$this->upload->do_upload('bbs_images'); 

and got 

Code:
Message: is_uploaded_file() expects parameter 1 to be string, array given


I also tried
PHP Code:
$this->upload->do_upload('bbs_images[0]'); 
 but it also returned false because do_upload would finally try to access $_FILES['bbs_images'][0] and finally got NULL.

So, How could I use Upload::do_upload since the structure of $_FILES['bbs_images'] is as follows ?


PHP Code:
array (size=5)
 
 'name' => 
 
   array (size=2)
 
     0 => string 'XXX.xls'
 
     1 => string 'XXX.sql'
 
 'type' => 
 
   array (size=2)
 
     0 => string 'XXX'
 
     1 => string 'XXX'
 
 'tmp_name' => 
 
   array (size=2)
 
     0 => string 'XXX'
 
     1 => string 'XXX'
 
 'error' => 
 
   array (size=2)
 
     0 => int 0
      1 
=> int 0
  
'size' => 
 
   array (size=2)
 
     0 => int XX
      1 
=> int XXX 
Reply
#2

I'm not used to use CI file upload but it seems you are in trouble since do_upload() seems to be made to one file only...

I would suggest:
1) Do a method to save $_FILES into another var and then try to override $_FILES with the proper structure and call do_upload on each file? Seems very clumsy but maybe it's possible to do this? I dunno...
2) Create another method similar to do_upload but prepared to receive that array of files.
3) Use another library like https://github.com/blueimp/jQuery-File-Upload
Reply
#3

Let me show you mine :

For the View :
Code:
<input type="file" multiple name="userfile[]" size="20"/>

For the controller :
Code:
$upload['upload_path'] = './uploads/';
$upload['allowed_types'] = 'gif|jpg|png';
$upload['max_size'] = 10000;
$upload['max_width'] = 4000;
$upload['max_height'] = 3000;

$files = $_FILES;
$count = count($_FILES['userfile']['name']);

for($i = 0; $i < $count; $i++) {
   $_FILES['userfile']['name']= $files['userfile']['name'][$i];
   $_FILES['userfile']['type']= $files['userfile']['type'][$i];
   $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
   $_FILES['userfile']['error']= $files['userfile']['error'][$i];
   $_FILES['userfile']['size']= $files['userfile']['size'][$i];    
   $this->upload->initialize($upload);
   $this->upload->do_upload();
}

Try upload some pics, if the pics you choose goes to 'uploads' folder, you did it
If it works for me, it should work for you too :)
Reply
#4

(08-21-2016, 11:08 AM)rikyperdana Wrote: Let me show you mine :

For the View :
Code:
<input type="file" multiple name="userfile[]" size="20"/>

For the controller :
Code:
$upload['upload_path'] = './uploads/';
$upload['allowed_types'] = 'gif|jpg|png';
$upload['max_size'] = 10000;
$upload['max_width'] = 4000;
$upload['max_height'] = 3000;

$files = $_FILES;
$count = count($_FILES['userfile']['name']);

for($i = 0; $i < $count; $i++) {
   $_FILES['userfile']['name']= $files['userfile']['name'][$i];
   $_FILES['userfile']['type']= $files['userfile']['type'][$i];
   $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
   $_FILES['userfile']['error']= $files['userfile']['error'][$i];
   $_FILES['userfile']['size']= $files['userfile']['size'][$i];    
   $this->upload->initialize($upload);
   $this->upload->do_upload();
}

Try upload some pics, if the pics you choose goes to 'uploads' folder, you did it
If it works for me, it should work for you too Smile

Hi, i have used your method and work very good but i don't know how to attach more files an e-mail. Can you help me please?

PHP Code:
$config['upload_path'         './media/images/uploads/';
 
               $config['allowed_types'       'gif|jpg|png|pdf';
 
               $config['max_size'            100;
 
               $config['max_width'           1024;
 
               $config['max_height'          768;

                
$files $_FILES;
                
$count count($_FILES['userfile']['name']);
                
                for(
$i 0$i $count$i++) {
                 
  $_FILES['userfile']['name'    $files['userfile']['name'][$i];
                 
  $_FILES['userfile']['type'    $files['userfile']['type'][$i];
                 
  $_FILES['userfile']['tmp_name'] = $files['userfile']['tmp_name'][$i];
                 
  $_FILES['userfile']['error'   $files['userfile']['error'][$i];
                 
  $_FILES['userfile']['size'    $files['userfile']['size'][$i]; 
 
  
                $this
->load->library('upload'$config);

 
               if ( ! $this->upload->do_upload('userfile'))
 
               {
 
                     $this->form_validation->set_error_delimiters('<p class="error">''</p>');
 
                     $error = array('error' => $this->upload->display_errors());
 
               }
 
               else
                
{
 
                    $upload_data $this->upload->data();
 
               }
 
               

$this->email->attach($upload_data['full_path']); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB