CodeIgniter Forums
slight problem with multiple file upload - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: slight problem with multiple file upload (/showthread.php?tid=24051)



slight problem with multiple file upload - El Forum - 10-29-2009

[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


slight problem with multiple file upload - El Forum - 10-29-2009

[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.


slight problem with multiple file upload - El Forum - 10-29-2009

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


slight problem with multiple file upload - El Forum - 10-30-2009

[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.....


slight problem with multiple file upload - El Forum - 11-17-2009

[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.


slight problem with multiple file upload - El Forum - 11-18-2009

[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.


slight problem with multiple file upload - El Forum - 11-18-2009

[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.


slight problem with multiple file upload - El Forum - 11-18-2009

[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...


slight problem with multiple file upload - El Forum - 01-08-2010

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