5

I am trying to upload multiple images and I have a jquery plugin set up so I can browse for multiple files and select them.

The only problem I am having is accessing the file array in my controller, does anyone know how I can do this? Thanks.

view:

<input type="file" name="userfile[]" id="userfile" class="multi" />

3 Answers 3

12

I had this same issue on a project last year, after a bit of searching I found a perfect function.

I take no credit it for it, but cannot remmeber where I found it so if anyone knows please link back to the author.

Make sure the form has the files named like this

<input type="file" name="userfile"/>
<input type="file" name="userfile2" />
<input type="file" name="userfile3" /> ..etc

Or

 <input type="file" name="userfile[]" />

The function..

function multiple_upload($upload_dir = 'uploads', $config = array())
{
    $files = array();

    if(empty($config))
    {
        $config['upload_path'] = '../path/to/file';
        $config['allowed_types'] = 'gif|jpg|jpeg|jpe|png';
        $config['max_size']      = '800000000';
    }

        $this->load->library('upload', $config);

        $errors = FALSE;

        foreach($_FILES as $key => $value)
        {            
            if( ! empty($value['name']))
            {
                if( ! $this->upload->do_upload($key))
                {                                           
                    $data['upload_message'] = $this->upload->display_errors(ERR_OPEN, ERR_CLOSE); // ERR_OPEN and ERR_CLOSE are error delimiters defined in a config file
                    $this->load->vars($data);

                    $errors = TRUE;
                }
                else
                {
                    // Build a file array from all uploaded files
                    $files[] = $this->upload->data();
                }
            }
        }

        // There was errors, we have to delete the uploaded files
        if($errors)
        {                    
            foreach($files as $key => $file)
            {
                @unlink($file['full_path']);    
            }                    
        }
        elseif(empty($files) AND empty($data['upload_message']))
        {
            $this->lang->load('upload');
            $data['upload_message'] = ERR_OPEN.$this->lang->line('upload_no_file_selected').ERR_CLOSE;
            $this->load->vars($data);
        }
        else
        {
            return $files;
        }
    } 

Now it's been a while since I've used it so if you need any extra help setting it up just let me know.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey, thanks, I've actually managed to figure it out...hooray!
I hate that Codeigniter does not have a support for multiple files input like <input type="file" name="userfile[]"/>. This "solution" is an actual workaround. But I must aprove that this solution is one of the best.
Hi, I would like to ask help on how to set this up when I just need it 1 page where my html form also reside. So I am self submitting my form. And what parameter should I pass on it. Thanks
1

To use Jquery multiple upload using html elements array [] developed as follows in the act of receiving data sobrescrevi the global variable $ _FILES PHP so that the function do_upload could read later. see:

            #copy the original array;               
            $temp_array;
            foreach($_FILES['fil_arquivo'] as $key => $val)
            {
                $i = 0;
                foreach($val as $new_key)
                {
                    $temp_array[$i][$key] = $new_key;
                    $i++;
                }
                //
            }

            $i = 0;
            foreach($temp_array as $key => $val)
            {
                $_FILES['file'.$i] = $val;
                $i++;
            }

            #clear the original array;
            unset($_FILES['fil_arquivo']);

            $upload_path = 'media/arquivos';

            $config['upload_path'] = $upload_path;
            $config['allowed_types'] = 'doc|docx|xls|xlsx|ppt|pptx|pdf|txt|jpg|png|jpeg|bmp|gif|avi|flv|mpg|wmv|mp3|wma|wav|zip|rar';
            $config['encrypt_name'] = true;

            $this->upload->initialize($config);
            foreach($_FILES as $key => $value)
            {            
                if( ! empty($value['name']))
                {
                    if($this->upload->do_upload($key))
                    {                                           
                        $this->upload->data();
                     }
                }
            }

1 Comment

wrong, they do not send the file name only to do_upload() method, its whole array
0

The HTML provided by ojjwood above will produce multiple input boxes. If you'd only like to have one input box but still upload multiple files, you need to include the attribute multiple="multiple" in your <input> element. The [] in name="userfile[]" allows the posted data to be an array and not just a single value.

On the Codeigniter/PHP side of things, you need to use $_POST['userfile] instead of $this->input->post('userfile') for working with arrays.

1 Comment

can you actually tell how will it actually work? everything inside $_FILES is also an array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.