Uploading multiple images by saving only one file

0

My code is saving only one image of X that I select in the input file, for example, if I select 3 or 10, only 1 image will be saved, I tried everything and I can not make it save all images in the folder ...

       public function add_multiple_images($images, $id, $resize = array('width' => 700, 'height' => 450)) {
        $total_images = count($_FILES['files']['name']);
        for ($i = 0; $i < $total_images; $i++) {
            if ($_FILES['files']['name'][$i] !== '') {
                $_FILES['files']['name'] = $_FILES['files']['name'][$i];
                $_FILES['files']['type'] = $_FILES['files']['type'][$i];
                $_FILES['files']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
                $_FILES['files']['error'] = $_FILES['files']['error'][$i];
                $_FILES['files']['size'] = $_FILES['files']['size'][$i];

                $config = array();
                $config['upload_path'] = "assets/uploads/locations/" . $id . "/";
                $config['allowed_types'] = '*';
                $config['max_size'] = '0';
                $config['file_name'] = random_name();
                $config['overwrite'] = FALSE;

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

                $this->upload->do_upload('files');

                $upload_data = $this->upload->data();
                if ($resize) {

                    $config['image_library'] = 'gd2';
                    $config['source_image'] = $upload_data['full_path'];
//                $config['create_thumb'] = TRUE;
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = $resize['width'];
                    $config['height'] = $resize['height'];

                    $this->load->library('image_lib', $config);

                    $this->image_lib->resize();
                    $this->image_lib->clear();
                }
            }
        }
    }

Form

    <form id="form_add_img_location" method="POST" action="<?= base_url('Admin/add_images_locations'); ?>" enctype="multipart/form-data">
      <input type="file" id="files" name="files[]" multiple>
</form>
    
asked by anonymous 27.07.2017 / 01:49

2 answers

1

When multiple files are attached to a <input type="file"> , $_FILES will come in this format:

/*
array(1) {
  ["file"]=>
      array(5) {
        ["name"]=>
            array(2) {
              [0]=> string(13) "alert-git.png"
              [1]=> string(15) "arquivo git.png"
            }
        ["type"]=>
            array(2) {
              [0]=> string(9) "image/png"
              [1]=> string(9) "image/png"
            }
        ["tmp_name"]=>
            array(2) {
              [0]=> string(14) "/tmp/phpdDHYVq"
              [1]=> string(14) "/tmp/phpGVjQUt"
            }
        ["error"]=>
            array(2) {
              [0]=> int(0)
              [1]=> int(0)
            }
        ["size"]=>
            array(2) {
              [0]=> int(73549)
              [1]=> int(76928)
            }
      }
}
*/

What you will have to go through array with foreach . Recently I posted a example that deals this problem and formats the array to make it easier to manipulate the data, thus:

/*
array(2) {
  [0]=> array(5)
    {
        ["name"]=> string(13) "alert-git.png"
        ["type"]=> string(9) "image/png"
        ["tmp_name"]=> string(14) "/tmp/phpdDHYVq"
        ["error"]=> int(0)
        ["size"]=> int(73549)
    }
  [1]=> array(5)
    {
        ["name"]=> string(15) "arquivo git.png"
        ["type"]=> string(9) "image/png"
        ["tmp_name"]=> string(14) "/tmp/phpGVjQUt"
        ["error"]=> int(0)
        ["size"]=> int(76928)
     }
}
*/
    
27.07.2017 / 13:41
1

It looks like you are overwriting the $_FILES array. When you do $_FILES['files']['name'] = $_FILES['files']['name'][$i]; , the number of available names will be only 1 (the first one assigned). You can create a copy of the $ _FILES array before you begin to change it. Example:

........

$imagens = $_FILES;
for ($i = 0; $i < $total_images; $i++) {
            if ($_FILES['files']['name'][$i] !== '') {
                $_FILES['files']['name'] = $imagens['files']['name'][$i];
                $_FILES['files']['type'] = $imagens['files']['type'][$i];
                $_FILES['files']['tmp_name'] =$imagens['files']['tmp_name'][$i];
                $_FILES['files']['error'] = $imagens['files']['error'][$i];
                $_FILES['files']['size'] = $imagens['files']['size'][$i];

........
    
27.07.2017 / 02:22