CodeIgniter Forums
Multiple file upload - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Multiple file upload (/thread-35123.html)



Multiple file upload - El Forum - 10-19-2010

[eluser]Thiago Leao[/eluser]
please, i need to Multiple file upload.

I have code, with upload simple, that generates two images, one at 800x600 and another 100x75


Code:
<?php
class Upload extends Controller {

  function go() {
    if(isset($_POST['upload'])) {
        
        $dia = date("Y-m");  
        $diretorio = 'assets/uploads/'.$dia.'/';
        
        if(!is_dir($diretorio)){
            mkdir($diretorio, 0777, TRUE);
            chmod($diretorio, 0777);
        }
        
        $config['upload_path'] = $diretorio;
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['remove_spaces'] = TRUE;
        $config['encrypt_name'] = TRUE;
        $config['max_size'] = '8192'; // 8Mbs
        
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        
        $this->upload->do_upload();    
        $image_data = $this->upload->data();
        
        $configLarge = array(
            'source_image' => $image_data['full_path'],
            'new_image' => $diretorio,
            'master_dim' => 'auto',
            'maintain_ratio' => true,
            'width' => 800,
            'height' => 600
        );
        
    
        $this->load->library('image_lib');
        $this->image_lib->initialize($configLarge);
        $this->image_lib->resize();        
        $this->image_lib->clear();
        
        $configThumb = array(
            'source_image' => $image_data['full_path'],
            'new_image' => $diretorio.'/'.substr($image_data['file_name'], 0, -4).'_thumb.'.end(explode(".", $image_data['file_name'])),
            'maintain_ratio' => true,
            'master_dim' => 'auto',
            'width' => 150,
            'height' => 150
        );        
    
        $this->image_lib->initialize($configThumb);
        $this->image_lib->resize();
            
    }
  }
}
?>

could someone help me how to do Multiple upload, please?
thanks!


Multiple file upload - El Forum - 10-19-2010

[eluser]pickupman[/eluser]
Here's a bit from the [url="http://php.net/manual/en/features.file-upload.multiple.php"]php manual[/url]. Taking this into CI, would be check if $_POST['userfile'] is an array, if so, loop through do_upload. Haven't tried it myself, as I usually use a jQuery uploader like uploadify.


Multiple file upload - El Forum - 10-19-2010

[eluser]Thiago Leao[/eluser]
I do not know what to do.
I looked at the forum and see the staff just creating thumb, has no like mine, which generates two images!


Multiple file upload - El Forum - 10-19-2010

[eluser]pickupman[/eluser]
You probably need to make sure you named you input names the same as an array.
Code:
<label for="userfile">Choose File</label>
&lt;?php echo form_upload('userfile[]');?&gt;<br/>
<label for="userfile">Choose File</label>
&lt;?php echo form_upload('userfile[]');?&gt;<br/>

//Controller
if(isset($_FILES['userfile'])){
   $files = $this->input->post('userfile');

   foreach($files as $file){
       //Do something here with the $file
   }
}



Multiple file upload - El Forum - 10-19-2010

[eluser]Thiago Leao[/eluser]
Code:
&lt;?php
class Upload extends Controller {

  function go() {
      
     if(isset($_FILES['userfile'])){
        
        $dia = date("Y-m");  
        $diretorio = 'assets/uploads/'.$dia.'/';
        
        if(!is_dir($diretorio)){
            mkdir($diretorio, 0777, TRUE);
            chmod($diretorio, 0777);
        }
        
        $config['upload_path'] = $diretorio;
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['remove_spaces'] = TRUE;
        $config['encrypt_name'] = TRUE;
        $config['max_size'] = '8192'; // 8Mbs
        
        $this->load->library('upload', $config);        
        
        
        $file_array = $_FILES['userfile'];
        $total_files = count($file_array['name']);
        // now we know the total number of files to upload.
        // let's built a fresh upload array...
        $files = array();
        for($i = 0; $i < $total_files; $i += 1) {
            $files[] = array(
                'name' => $file_array['name'][$i],
                'tmp_name' => $file_array['tmp_name'][$i],
                'size' => $file_array['size'][$i],
                'error' => $file_array['error'][$i]
            );
        }
        
        foreach($files as $file){

        //$image_data = array('upload_data' => $this->upload->data());
        $this->upload->do_upload();    
        $image_data = $this->upload->data();
        
        $configLarge = array(
            'source_image' => $image_data['full_path'],
            'new_image' => $diretorio,
            'master_dim' => 'auto',
            'maintain_ratio' => true,
            'width' => 800,
            'height' => 600
        );
        
    
        $this->load->library('image_lib');
        $this->image_lib->initialize($configLarge);
        $this->image_lib->resize();        
        $this->image_lib->clear();
        
        echo "<pre>";
        print_r($configLarge);
        echo "</pre>";
        
        $configThumb = array(
            'source_image' => $image_data['full_path'],
            'new_image' => $diretorio.'/'.substr($image_data['file_name'], 0, -4).'_thumb.'.end(explode(".", $image_data['file_name'])),
            'maintain_ratio' => true,
            'master_dim' => 'auto',
            'width' => 150,
            'height' => 150
        );        
    
        $this->image_lib->initialize($configThumb);
        $this->image_lib->resize();

        }
        
        
        
            
    }
  }
}
?&gt;

does not show error, but also does not send!


Multiple file upload - El Forum - 10-20-2010

[eluser]pickupman[/eluser]
You should be using syntax similar to the user guide
Code:
if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
            
            print_r($error);
        }    
        else
        {
            $data = array('upload_data' => $this->upload->data());
            
            $this->load->view('upload_success', $data);
        }

That way you know whether or not the file has been uploaded or not. It looks like you are on the right track.