CodeIgniter Forums

Full Version: redirect issue file upload.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I can upload multiple images fine on my upload function if comment out my redirect on my upload function.

How ever if I enable the redirect it will only upload one for some reason very strange to me.

When upload is success full I would like to use redirect('filemanager')

Any suggestion and example why the redirect would stop multiple upload.

PHP Code:
<?php

class Filemanager extends CI_Controller {

public function 
__construct() {
    
parent::__construct();
    
$this->load->library('upload');
}

public function 
index() {
    
$data['title'] = 'File Manager';

    
$data['template'] = 'template/common/filemanager_view';

    
$this->load->view('template/common/template_view'$data);
}

public function 
upload() {

    
$files $_FILES;

    if (
$files) {

        
$cpt count($_FILES['userfile']['name']);
                     
   
        
for($i=0$i<$cpt$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];  

            $url 
'';

            
$directory $this->input->get('directory');

            if (isset(
$directory)) {
                
$url .= $directory '/';
            } else {
                
$url .= '';
            } 
 

            $config
['upload_path'] = FCPATH 'images/catalog/' $url;
         
   $config['allowed_types'] = 'gif|jpg|png';
         
   $config['max_size'] = 5000;
         
   $config['max_width'] = 0;
         
   $config['max_height'] = 0;
         
   $config['overwrite'] = TRUE;

            
$this->upload->initialize($config);
                
            if (
$this->upload->do_upload() == TRUE) {
                
//redirect('filemanager');
            
}
        }

    }

    
$data['error'] = '';
 
   $data['template'] = 'template/common/upload_view';
    
$this->load->view('template/common/template_view'$data);

}


You are redirecting from inside your loop. So indeed you get redirected after the first files is procesed. You want to redirect after the loop is finished so you should place it below.
(02-18-2016, 01:42 AM)Diederik Wrote: [ -> ]You are redirecting from inside your loop. So indeed you get redirected after the first files is procesed. You want to redirect after the loop is finished so you should place it below.

Does this seem better to you

PHP Code:
<?php

class Filemanager extends CI_Controller {

    public function 
__construct() {
        
parent::__construct();
        
$this->load->library('upload');
    }

    public function 
index() {
        
$data['title'] = 'File Manager';

        
$upload_errors $this->upload->display_errors();

        if (isset(
$upload_errors)) {
            
$data['upload_errors'] = $upload_errors;
        } else {
            
$data['upload_errors'] = '';
        }

        
$data['template'] = 'template/common/upload_view';
        
$this->load->view('template/common/template_view'$data);
    }

    public function 
upload() {

        
$files $_FILES;

        if (
$files) {

            
$check_if_any_error 0;

            
$cpt count($_FILES['userfile']['name']);
                     
   
            
for($i=0$i<$cpt$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];  

                $url 
'';

                
$directory $this->input->get('directory');

                if (isset(
$directory)) {
                    
$url .= $directory '/';
                } else {
                    
$url .= '';
                } 
 

                $config
['upload_path'] = FCPATH 'images/catalogs/' $url;
         
       $config['allowed_types'] = 'gif|jpg|png';
         
       $config['max_size'] = 5000;
         
       $config['max_width'] = 0;
         
       $config['max_height'] = 0;
         
       $config['overwrite'] = TRUE;

                
$this->upload->initialize($config);

                
$this->upload->do_upload();

                
$check_if_any_error++;
                
            }

            if(
$check_if_any_error ) {

 
               $this->index();

 
           } else {

 
               redirect('filemanager');

 
           }
        }
 
       
    
}

You would improve it by:
PHP Code:
if( ! $this->upload->do_upload() ) {
    
$check_if_any_error++;    

That way you will only increment the error counter when an error actually occurs.