Codeigniter & HTML5 - trying to load multiple images at the same time

View looks like this

        <input type="file" multiple="multiple" name="userfile[]" id="userfile" />
        <?=form_submit('upload', 'Upload');?>


I want the user to be able to upload multiple images at once. after uploading, I want to record the details of the image in the database and finally move the image to the uploads folder.

I have basic knowledge of coding

ps: I don't want to use upload or similar image uploads. trying to keep it as light as possible

Update this is the array I get when I try var_dump ($ _ FILES ['userfile']). which loop should I use to split the data of the respective images.

  'name' => 
      0 => string '01.jpg' (length=6)
      1 => string '1 (26).jpg' (length=10)
  'type' => 
      0 => string 'image/jpeg' (length=10)
      1 => string 'image/jpeg' (length=10)
  'tmp_name' => 
      0 => string 'C:\wamp\tmp\php2AC2.tmp' (length=23)
      1 => string 'C:\wamp\tmp\php2AD3.tmp' (length=23)
  'error' => 
      0 => int 0
      1 => int 0
  'size' => 
      0 => int 409424
      1 => int 260343



source to share

3 answers

I faced this problem. The data is $_FILES

assigned a clear structure (due to the attribute multiple=""

), so that the codignitor cannot process it. Prepare this before the boot process:

$arr_files  =   @$_FILES['userfile'];

$_FILES     =   array();
foreach(array_keys($arr_files['name']) as $h)
$_FILES["file_{$h}"]    =   array(  'name'      =>  $arr_files['name'][$h],
                                    'type'      =>  $arr_files['type'][$h],
                                    'tmp_name'  =>  $arr_files['tmp_name'][$h],
                                    'error'     =>  $arr_files['error'][$h],
                                    'size'      =>  $arr_files['size'][$h]);


Then, in your loop function, use this:


$arr_config =   array(  'allowed_types' =>  'gif|jpg|png',
                            'upload_path'   =>  'url_path/');

foreach(array_keys($_FILES) as $h) {

    // Initiate config on upload library etc.


    if ($this->upload->do_upload($h)) {

        $arr_file_data  =   $this->upload->data();




I just change the struct $_FILES

to a generic struct that is dispatched by default <input type="file" />

and starts a loop retrieving all of their key names.



Required cycle:

for($i=0; $i<count($_FILES['name']); $i++){
    if ($_FILES['error'][$i] == 0) {
        //do your stuff here, each image is at $_FILES['tmp_name'][$i]


Note that there is no CI load class used, but plain PHP which I usually find it easier to work with rather than the CI class.



Check out this class for multiple file upload

It helped me.



All Articles