EllisLab text mark
Advanced Search
     
Multiple file upload problem
Posted: 15 November 2012 06:34 PM   [ Ignore ]
Joined: 2011-04-15
28 posts

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:

$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 
'<input type="file" name="pic[' $i ']" id="pic[' $i ']" />';
                                                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:

$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!

 
Posted: 15 November 2012 06:56 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2011-07-24
3 posts

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:

// 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
 
Posted: 15 November 2012 07:04 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-04-15
28 posts

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.

 
Posted: 15 November 2012 07:17 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2011-07-24
3 posts

If you want to know which file is faulty one you can add some code into cycle:

// 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 simple:)

 
Posted: 15 November 2012 07:20 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2011-04-15
28 posts

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.

 
Posted: 19 November 2012 06:48 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2011-04-15
28 posts

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

$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!