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">
3
  • Add your input form code.. Commented Mar 29, 2020 at 16:46
  • Can you upload files successfully ? However , you can get the file name by './uploads/'.$imagename Commented Mar 29, 2020 at 16:50
  • No, files are not upload Commented Mar 29, 2020 at 17:00

2 Answers 2

2

Replace the Line

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

to

if(!empty($_FILES['files']['name'][$i]))
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.