CodeIgniter Forums
Multiple file upload problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Multiple file upload problem (/thread-55902.html)



Multiple file upload problem - El Forum - 11-15-2012

[eluser]zsela[/eluser]
Hi Everyone,

I am pretty new at coding with CodeIgniter, but I pretty much enjoy it. However I ran into a problem on the other day which is related to multiple file uploading. My code doesn't want to work, when I want to upload multiple pictures and some additional input fields at the same time. I use an array as name for the pictures, but at the controller side the array seems to be empty. What is strange is that the $_FILE array does contain the uploaded pictures.

Here is the relevant part of the view:

Code:
$attributes_pic = array('id' => 'upload_real_pic_form');
                        echo form_open_multipart('realestate/upload_photo/' . $page_data['realestate_ID'], $attributes_pic);

                                        echo '<td class="centerMediumCol" colspan="2">';
                                        echo '&lt;input type="file" name="pic[' . $i . ']" id="pic[' . $i . ']" /&gt;';
                                                if (isset($page_data['pic' . $i . '_error']) && $page_data['pic' . $i . '_error']) {
                                                        
                                                        echo '<br /><br />';
                                                        foreach ($page_data['pic' . $i . '_error'] as $v) {
                                                                echo $v;
                                                        }
                                                        
                                                }

And here is the controller:

Code:
$config['upload_path'] = './images/uploads/realestates/';
      $config['overwrite'] = TRUE;
      $config['allowed_types'] = 'gif|jpg|png';
      $config['max_size'] = '1024';
      $config['max_width']  = '4000';
      $config['max_height']  = '3000';
      
      $this->load->library('upload');
      
      $i = 1;
      $success = 0;
      foreach($this->upload->data('pic') as $k => $image) {
      
       $index = ($i < 10)?'0'.$i:$i;
       $config['file_name'] = 'realestate_' . $realestate_ID . '_' . $index;
       $this->upload->initialize($config);
      
       echo $config['file_name'];
      
       if (!$this->upload->do_upload($image)) {
        
        // hiba volt a feltöltésnél
        $page_data['pic' . $k . '_error'] = array('upload_error' => $this->upload->display_errors('<p class="error">', '</p>'));
        
        $page_data['realestate_ID'] = $realestate_ID;
        $page_data['pic_limit'] = $data['user_limit_pic'];      
        $this->load->view('realestate/upload_picture', array('page_data' => $page_data));
        
       } else {
        
        $pr = ($primary == $k)?1:0;
        
        $pic_data = array('upload_data' => $this->upload->data());
        $this->Realestate_model->del_realestate_pictures($realestate_ID);
        $this->Realestate_model->upload_realestate_pictures($realestate_ID, $pic_data['upload_data']['file_name'], $type[$i], $description[$i], $pr);
        
        $success = 1;
        
       }
      
       $i++;
      
      }

If I printout $this->upload->data('pic') it is an empty array.
What do I do wrong?

Thanks for your replies in advanced!


Multiple file upload problem - El Forum - 11-15-2012

[eluser]homebwoi[/eluser]
I'm not sure but as I know File Uploading library doesn't support multiple files upload, so there is the way I solve this problem:

Code:
// Get count of files
$count = count($_FILES['pic']['name']);

// Upload each file separately
for ($i = 0; $i < $count; $i++)
{
  $_FILES['singlefile']['name'] = $_FILES['pic']['name'][$i];
  $_FILES['singlefile']['type'] = $_FILES['pic']['type'][$i];
  $_FILES['singlefile']['tmp_name'] = $_FILES['pic']['tmp_name'][$i];
  $_FILES['singlefile']['error'] = $_FILES['pic']['error'][$i];
  $_FILES['singlefile']['size'] = $_FILES['pic']['size'][$i];
      
  $this->upload->do_upload('singlefile');

  // Some actions with uploaded file
}



Multiple file upload problem - El Forum - 11-15-2012

[eluser]zsela[/eluser]
Interesting idea. And how do you know, if something goes wrong with the upload (for example unsupported file type) which file was the faulty one?

I looked at this topic, and it suggested me that multiple upload is somehow supported.


Multiple file upload problem - El Forum - 11-15-2012

[eluser]homebwoi[/eluser]
If you want to know which file is faulty one you can add some code into cycle:

Code:
// Upload each file separately
for ($i = 0; $i < $count; $i++)
{
  $_FILES['singlefile']['name'] = $_FILES['pic']['name'][$i];
  $_FILES['singlefile']['type'] = $_FILES['pic']['type'][$i];
  $_FILES['singlefile']['tmp_name'] = $_FILES['pic']['tmp_name'][$i];
  $_FILES['singlefile']['error'] = $_FILES['pic']['error'][$i];
  $_FILES['singlefile']['size'] = $_FILES['pic']['size'][$i];
      
  if ($this->upload->do_upload('singlefile'))
  {
    // Some actions with uploaded file
  } else {
    echo "The error was occurred when uploading file with nubmer " . $i;
    // Or simething like that:)
  }
}

Maybe there are more efficient solutions, but I think that one is pretty simpleSmile


Multiple file upload problem - El Forum - 11-15-2012

[eluser]zsela[/eluser]
Thanks for the quick replies!
Unfortunately I wiil only have time to try this at the weekend, but as soon as I try it will post hte results here.


Multiple file upload problem - El Forum - 11-19-2012

[eluser]zsela[/eluser]
Thanks for the tip again, it worked fine.
Here is the solution, it might be handy for others in the future:

Code:
$config['upload_path'] = './images/uploads/realestates/';
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['max_width']  = '4000';
$config['max_height']  = '3000';
      
$this->load->library('upload');
      
$count = count(array_filter($_FILES['pic']['name']));
      
$this->Realestate_model->del_realestate_pictures($realestate_ID);
      
// upload files one-by-one
for ($i = 1; $i <= $count; $i++) {

  $_FILES['singlefile']['name'] = $_FILES['pic']['name'][$i];
  $_FILES['singlefile']['type'] = $_FILES['pic']['type'][$i];
  $_FILES['singlefile']['tmp_name'] = $_FILES['pic']['tmp_name'][$i];
  $_FILES['singlefile']['error'] = $_FILES['pic']['error'][$i];
  $_FILES['singlefile']['size'] = $_FILES['pic']['size'][$i];
        
  $index = ($i < 10)?'0'.$i:$i;
  $config['file_name'] = 'realestate_' . $realestate_ID . '_' . $index;
  $this->upload->initialize($config);
            
  if ($this->upload->do_upload('singlefile')) {
          
    $pr = ($primary == $i)?1:0;
        
    $pic_data = array('upload_data' => $this->upload->data());
    $this->Realestate_model->upload_realestate_pictures($realestate_ID, $pic_data['upload_data']['file_name'], $type[$i], $description[$i], $pr);
          
  } else {
        
    $page_data['pic' . $i . '_error'] = array('upload_error' => $this->upload->display_errors('<p class="error">', '</p>'));
      
    $page_data['realestate_ID'] = $realestate_ID;
    $page_data['pic_limit'] = $data['user_limit_pic'];      
    $this->load->view('realestate/upload_picture', array('page_data' => $page_data));
        
    return 0;
        
  }
        
)

Issue is closed!