1

Here I am attaching the code of the desires problem. Cotroller has following code. Controller=>

//Load upload library
            $this->load->library('upload');
            $images = array();
            $i = 0;                                                                 
            foreach ($_FILES as $key => $value) 
            {                                                                
                $tmp = explode(".",$value['name'][$i]);                
                $imagename = time().".".end($tmp); 

                $_FILES['file']['name']     = $imagename;
                $_FILES['file']['type']     = $value['type'][$i];
                $_FILES['file']['tmp_name'] = $value['tmp_name'][$i];
                $_FILES['file']['error']    = $value['error'][$i];
                $_FILES['file']['size']     = $value['size'][$i];

                $config['upload_path']   = './uploads/'; 
                $config['allowed_types'] = 'gif|jpg|png';
                $config['file_name']     = $imagename;                                

                $this->upload->initialize($config);                                        

                if ( ! $this->upload->do_upload('file')) 
                {
                    $error = array($i => $this->upload->display_errors());                 
                    echo "<pre>";print_r($error);die;
                }            
                else 
                { 
                    array_push($images,$this->upload->data()['file_name']);                 
                }        
                $i++;                
            }            
            echo "<pre>";print_r($images);die;

This is a form code that I am using while uploading file. View =>

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

Here is my file input control.

<label for="file">Profile Images*</label>
<input type="file" name="files[]" id="file" multiple required placeholder="Profile Images" class="form-control">
  • 1
    What about [this question](https://stackoverflow.com/a/60917790/12731030) ? – Dum Mar 30 '20 at 08:50
  • 2
    Does this answer your question? [How to upload multiple images using codeigniter](https://stackoverflow.com/questions/19317418/how-to-upload-multiple-images-using-codeigniter) – Triby Mar 30 '20 at 16:07
  • 1
    Does this answer your question? [I am getting empty array, I need array of image while uploading multiple images in Codeigniter](https://stackoverflow.com/questions/60917320/i-am-getting-empty-array-i-need-array-of-image-while-uploading-multiple-images) – AMC Mar 30 '20 at 18:28

3 Answers3

2

Change your code as follows

foreach($_FILES["files"]["tmp_name"] as $key=>$value) {

and change $i to the $key as follows (apply to the all)

$_FILES['file']['type']     = $_FILES["files"]['type'][$key];

As wazabii suggested, attached some random string to the file name. You can use rand(100,10000)

Dum
  • 1,431
  • 2
  • 9
  • 23
  • 1
    Take a look at [this documentation](https://www.php.net/manual/en/features.file-upload.post-method.php). – Dum Mar 30 '20 at 08:59
  • Are you sure ? Because while I am passing tmp_name it will consider only tmp_name in loop. – Mehul Dhariyaparmar Mar 30 '20 at 09:06
  • 1
    Yes, it only consider `tmp_name` when looping. But, In order to grab data, you should use `$_FILES["files"]['type'][$key]`. Try `print_r()`, you will see. – Dum Mar 30 '20 at 10:12
1

That is because the time() will be the same on all the images, so the file name is not unique. This is easily fixed by adding the array key to the file name.

$tmp = explode(".",$value['name'][$i]);
$imagename = time()."-".$key.".".end($tmp);
wazabii
  • 224
  • 3
  • 7
1

contoller code

public function upload_multiple($field_name,$path){

    $this->load->library('upload');
    $files = $_FILES;
    $cpt = count($_FILES[$field_name]['name']);//count for number of image files
    $image_name =array();
    for($i=0; $i<$cpt; $i++)
    {           
        $_FILES[$field_name]['name']= $files[$field_name]['name'][$i];
        $_FILES[$field_name]['type']= $files[$field_name]['type'][$i];
        $_FILES[$field_name]['tmp_name'] = $files[$field_name]['tmp_name'][$i];
        $_FILES[$field_name]['error']= $files[$field_name]['error'][$i]; 
        $_FILES[$field_name]['size'] = $files[$field_name]['size'][$i];
    
        $this->upload->initialize($this->set_upload_options($path));//for initalizing configuration for each image
        $this->upload->do_upload($field_name);  

        $data = array('upload_data' => $this->upload->data()); 
        $image_name[]=$data['upload_data']['file_name'];//store file name to store in database
    }
    return $image_name;//all images name which is uploaded
}
public function set_upload_options($path)
{   
    $config = array();
    $config['upload_path'] = $path;
    $config['allowed_types'] = 'gif|jpg|png';
    $config['overwrite']     = FALSE;

    return $config;
}

function call

  $image_name=$this->upload_multiple('portfolio_image',$path);//for multiple image upload

input field

 <input type="file" id="portfolio_image" name="protfolio_image[]" >