Upload multiple images by saving only one file

Asked

Viewed 251 times

0

My code is saving only an X image that I select in the input file, for example, if I select 3 or 10, only 1 image will be saved, I’ve tried everything and I can’t get it to save all the 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>

2 answers

1

Looks like you’re overwriting the array $_FILES. When you do $_FILES['files']['name'] = $_FILES['files']['name'][$i];, the amount of available Names will only be 1 (the first one assigned). You can create a copy of the $_FILES array before you start changing 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];

........

1


When multiple files are attached in one <input type="file">, the $_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. I recently published a example which treats this problem and formats the array for easy handling of 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)
     }
}
*/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.