CodeIgniter Forums

Full Version: slight problem with multiple file upload
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]eokorie[/eluser]
I am currently using the code below in an attempt to upload multiple files.

Code:
function do_upload()
    {
        
        $this->folder = './uploads/';
        $this->dynamicFolder = get_realpath(APPPATH.$this->folder);
        
        $config['upload_path'] = $this->dynamicFolder;
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size'] = '1000';
        $config['max_width'] = '1920';
        $config['max_height'] = '1280';                        
        
        // $this->load->library('upload', $config);
        $this->upload->initialize($config);
        $field = 'userfile';
        
        if(isset ($_FILES[$field]) and is_array($_FILES[$field]['error']) )
        {
            $count = count($_FILES[$field]['error']);
            for ($i = 0; $i < $count; $i++)
            {
                $data = null;
                
                $pseudo_field_name = '_psuedo_'. $field .'_'. $i;
                
                $_FILES[$pseudo_field_name] = array(
               'name' => $_FILES[$field]['name'][$i],
               'size' => $_FILES[$field]['size'][$i],
               'type' => $_FILES[$field]['type'][$i],
               'tmp_name' => $_FILES[$field]['tmp_name'][$i],
               'error' => $_FILES[$field]['error'][$i]
                );
                
                $this->upload->do_upload($pseudo_field_name);
                $data = $this->upload->data();

                $success[$i] = $data['file_name'];
                
                $this->_createThumbnail_small($data['file_name']);
                
                $data['uploadInfo'] = $data;
                $data['thumbnail_small'] = $data['raw_name'] . '_small' . $data['file_ext'];
                
                $this->session->set_flashdata('notice',$success[$i]);
                $this->load->view('my_upload_success',$data);

            }
        }else{
            
            if(!$this->upload->do_upload($field))
            {
                //$success = array('error' => $this->upload->display_errors());
                //$this->session->set_flashdata('notice',$success);
                // $this->load->view('my_upload');
                //redirect(base_url().'my_upload','location');
                echo $this->upload->display_errors();
            }
            else {
                $data = $this->upload->data();
                $success = $data['file_name'];
                //$this->session->set_flashdata('notice',$success);
                //redirect(base_url().'my_upload_success','location');
            }
        }
    }

I opted to use the JQuery Multifile plugin as Uploadify just was too much work. SO far things are going well but I am hitting a couple of snags. If I upload just one file, things go well. When I try to upload say 3 files for example, the first file that is uploaded retains its filename, but the last two don't. i.e. first file upload keeps it name of say desert.jpg. But the second file called tulips.jpg gets renamed to desert.jpg.jpg and the 3rd file gets renamed to desert.jpg.jpg.jpg.

Anyone have a solution to this problem? Thanks

El Forum

[eluser]Ben Edmunds[/eluser]
Which version of CI are you using?

There was a bug in the naming on old versions of the file uploader.

El Forum

[eluser]eokorie[/eluser]
I am using 1.7.2.

El Forum

[eluser]eokorie[/eluser]
Actually found the mentioned bug in the upload.php library file. On line 935. Anyway, I commented that line out and everything is working A-OK.....

El Forum

[eluser]Peter Lachky[/eluser]
Hi guys,

thanks for this topic - I have spent ages wondering what have I done wrong - have got the same problem and this solution did help me. But I am afraid a bit whether the commented line could not break something else? Eokorie, does your upload work fine, did you test it?

Cheers,
Pete.

El Forum

[eluser]eokorie[/eluser]
Hi Peter,

The upload seems to work well for the purpose I had designed for it. If you need a copy of the code I wrote, just pm me.

El Forum

[eluser]Peter Lachky[/eluser]
Hi Eokorie,

after the patch in upload.php library file works fine here as well. I am just a bit afraid of patching CI files, that's all.

P.

El Forum

[eluser]Ben Edmunds[/eluser]
Hey Peter,

Just to ease your mind I've patched it the same and have been using it on a production website with no problems.

I am very surprised the bug hasn't been officially patched yet though...

El Forum

[eluser]anthropos9[/eluser]
@eokorie thanks for the info. Been racking my brain about this all afternoon. Your patch fixed it.