1

SOLVED I have a file field named additional_photos[] on a web page, there can be n number of instances of this field of course with different ID but same name. In a normal PHP code I will do a foreach on $_FILES['additional_photos'] and will do rest easily. But how do I achieve the same with CodeIgniter? I tried doing this:

        $additional_photosCount = count($_FILES['additional_photos']['name']); //Is there a better way to refer $_FILES like I can refere $_POST in CI?
        for($i=0; $i< $additional_photosCount; $i++){
            $uploadConfig['file_name'] = $this->properties['userId'].'-'.$_FILES['additional_photos']['name'][$i];
            $this->CI->upload->initialize($uploadConfig);
            if(!$this->CI->upload->do_upload('additional_photos['.$i.']')){;
                echo $this->CI->upload->display_errors();
            }
        }

But this,
a) IMHO, isn't correct way
b) gives me error "You did not select a file to upload."
Update
This is a way out I could apply:

        $additional_photosCount = count($_FILES['additional_photos']['name']);
        for($i=0; $i< $additional_photosCount; $i++){
            $uploadConfig['file_name'] = $this->properties['userId'].'-'.$_FILES['additional_photos']['name'][$i];
            $this->CI->upload->initialize($uploadConfig);
            $_FILES['additional_photos_'.$i] = array(
                'tmp_name'=> $_FILES['additional_photos']['tmp_name'][$i],
                'name'=> $_FILES['additional_photos']['name'][$i],
                'type'=> $_FILES['additional_photos']['type'][$i],
                'size'=> $_FILES['additional_photos']['size'][$i],
                'error'=> $_FILES['additional_photos']['error'][$i],
            );
            if(!$this->CI->upload->do_upload('additional_photos_'.$i)){;
                echo $this->CI->upload->display_errors();//TODO: instead of echoing push errors to a list
            }
        }

2 Answers 2

1

Check this : https://github.com/nicdev/CodeIgniter-Multiple-File-Upload , should do what you want above.

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

2 Comments

toopay, thanks for the tip, I am looking at the extension but at first look it seems it will help me, for every file I also need to perform some extra operations, noting down file name and resizing, so it may not help me that way, but still thanks
You easily can extend or even change that class, and adding a function to do what you need.
0

in HTML5 you can use this functionality

If you want to let the user select multiple files, simply use the multiple attribute on the input element:

   <input type="file" id="input" multiple onchange="handleFiles(this.files)">

REFERENCE

1 Comment

I am more concerned about server side handling. I am not using HTML5 but even in case of HTML5, how will you handle multiple files on server side if you have single element name, not ID, of file input, look at this codeigniter.com/user_guide/libraries/file_uploading.html

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.