1

This code is for getting file name in array. I am getting an empty array of file. Here I am attaching the source code for the same issue. Have look a code and give me some solution

            $images = array();    

            $count = count($_FILES['files']['name']);

            for($i = 0; $i < $count ; $i++)
            {                 

                if(!empty($_FILES['file']['name'][$i]))
                {

                    $tmp = explode(".",$_FILES['files']['name'][$i]);
                    $file_extension = end($tmp); //this is temp variable

                    $imagename = time().".".$file_extension;                                    

                    // Define new $_FILES array - $_FILES['file']
                    $_FILES['file']['name'] = $_FILES['files']['name'][$i];
                    $_FILES['file']['type'] = $_FILES['files']['type'][$i];
                    $_FILES['file']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
                    $_FILES['file']['error'] = $_FILES['files']['error'][$i];
                    $_FILES['file']['size'] = $_FILES['files']['size'][$i];

                    // Set preference
                    $config['upload_path'] = './uploads/'; 
                    $config['allowed_types'] = 'jpg|jpeg|png|gif';   // this is allowed file type                 
                    $config['file_name'] = $imagename;

                    //Load upload library
                    $this->load->library('upload',$config);                                                     
                    if($this->upload->do_upload('file')){
                        // Get data about the file
                        $uploadData = $this->upload->data();
                        $filename = $uploadData['file_name'];

                        // Initialize array
                        array_push($images, $filename);
                    }                    
                }
            }
            echo "<pre>";print_r($images);die;

Also here I am attaching form code

 <?php $attributes = array(
 "class"                 => "form-horizontal m-t-20",
 "method"                => "post",
 "novalidate"            => "",
 "enctype"               => "multipart/form-data"
 );
 echo form_open('admin/user/adduser', $attributes); ?>

This code for file input
<label for="file">Profile Images*</label>
<input type="file" name="files[]" id="file" multiple required placeholder="Profile Images" class="form-control">

2 Answers2

2

Replace the Line

if(!empty($_FILES['file']['name'][$i]))

to

if(!empty($_FILES['files']['name'][$i]))
Mohammed Shafeek
  • 1,846
  • 2
  • 13
  • 24
1

Change files to files[]. You can read about more here.

Suggestion

Move this line $this->load->library('upload',$config) out of the loop, because each iteration CI will try to load upload library which is already loaded. That may affect site's perfirmance. You can use initialize() function.

Dum
  • 1,283
  • 2
  • 4
  • 21