Welcome Guest, Not a member yet? Register   Sign In
Multiple File Upload
#1

[eluser]Thiago Leao[/eluser]
I'm trying to send 5 images, but only sends 1.

Anyone know where I'm wrong?

Thanks

Code:
<?php
echo form_open_multipart(base_url().'administracao/upload/go')."\n";
echo form_hidden('id_products', $dados_produto[0]->id_products)."\n";
                
echo form_label('Imagem', 'userfile_1')."\n";
                
echo form_upload('userfile_1');
echo form_upload('userfile_2');
echo form_upload('userfile_3');
echo form_upload('userfile_4');
echo form_upload('userfile_5');

echo form_label('Descrição', 'descricao')."\n";
echo form_input('descricao', set_value('descricao'))."\n";
                
echo form_submit('upload');
echo form_close();
?>

Code:
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);
        }
        
        for ($i = 1; $i <= 5; $i++){
            

            
                //print_r($_FILES);
                
                            
                $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('userfile_'.$i);    
                $image_data = $this->upload->data();
                
                $configLarge = array(
                    'source_image' => $diretorio.$image_data['file_name'],
                    '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' => false,
                    'master_dim' => 'auto',
                    'width' => 120,
                    'height' => 90
                );    
                
                $this->image_lib->initialize($configThumb);
                $this->image_lib->resize_and_crop();
                
                /*GRAVAR IMAGEM NO BANCO*/
                
                $data['id_products']     = $this->input->post('id_products');
                $data['descricao']         = $this->input->post('descricao');
                $data['thumb']            = $configThumb['new_image'];
                $data['imagem']            = $configLarge['source_image'];
                
                $this->load->model('administracao/cad_produto_model');
                $this->cad_produto_model->cadastrar_img($data);
                redirect(base_url().'administracao/cad_produto/upload/'.$data['id_products'], 'refresh');
            //}
        }
    }
  }
#2

[eluser]AlexJ[/eluser]
I think you are redirecting in the for loop?
#3

[eluser]darrentaytay[/eluser]
I done a simple blog post on this for 2 images - should be easily adapted for 5:

http://darrenonthe.net/2011/05/08/upload...deigniter/
#4

[eluser]phpxpert[/eluser]
After uploading one image you are redirecting the user to another page by the code

Code:
redirect(base_url().'administracao/cad_produto/upload/'.$data['id_products'], 'refresh');

so place the redirecting command after the for loop
#5

[eluser]Thiago Leao[/eluser]
hi friends, actually, I forgot to get the redirect loop!
thank you all!
#6

[eluser]Thiago Leao[/eluser]
Hi guys, I'm having another problem!!

I'm trying to send 3 images.

but it attempts to send 5, and userfile_4 undefined.

I'm finding that my count is wrong!!

A PHP Error was encountered
Severity: Notice
Message: Undefined index: userfile_4
Filename: administracao/upload.php
Line Number: 17



Code:
for ($i = 1; $i < count($_FILES['userfile_'.$i]); $i++) {
#7

[eluser]Thiago Leao[/eluser]
I did it!

for ($i = 0; $i < count($_FILES); $i++)




Theme © iAndrew 2016 - Forum software by © MyBB