0

I want to upload 1 specific index from an array of images from POST? From data below, let's say I only want to upload index 2.

// $_FILES['image']
Array 
(
    [name] => Array
        (
            [1] => 2b.png
            [2] => 2c.png
        )

    [type] => Array
        (
            [1] => image/png
            [2] => image/png
        )

    [tmp_name] => Array
        (
            [1] => C:\xampp\tmp\php247F.tmp
            [2] => C:\xampp\tmp\php2480.tmp
        )

    [error] => Array
        (
            [1] => 0
            [2] => 0
        )

    [size] => Array
        (
            [1] => 244274
            [2] => 277569
        )

)

So far I got:

if ($this->upload->do_upload('image[2]'))
...

But obviously it doesn't work that way. any idea how?

I have a bigger loop before the images and I only have to select 1 image per iteration. It will be a big overhead if I use another loop to select an image.

ACD
  • 1,431
  • 1
  • 8
  • 24
  • foreach through $_FILES and insert when key = 1 – Devsi Odedra Nov 05 '18 at 09:17
  • is there a way not to loop through it? it's a big overhead if I had a lot of images and iterations. I have a bigger loop before the images and I only have to select 1 image per iteration. – ACD Nov 05 '18 at 09:22
  • Possible duplicate of [Multiple image upload with CodeIgniter](https://stackoverflow.com/questions/40778683/multiple-image-upload-with-codeigniter) – Alex Nov 05 '18 at 09:30
  • I don't want to upload MULTIPLE images, I just want to pick one. I have to get rid of the loop.@Alex – ACD Nov 05 '18 at 09:34
  • _“I don't want to upload MULTIPLE images“_ - but at this point, you already _have_ uploaded multiple images to the server (otherwise, you would not have such a structure in $_FILES to begin with.) If that is not the purpose of the whole thing, than why are you wasting bandwidth and traffic by letting the user upload multiple images/files in the first place, if you are going to throw away all of them but one? – misorude Nov 05 '18 at 09:37
  • let's say logic condition can only be done server side. Besides, all images are uploaded but they have to meet specific condition for each iteration. @misorude – ACD Nov 05 '18 at 09:40

1 Answers1

0

You can try this for multiple file upload:

$index=2;
if(is_uploaded_file($_FILES['image']['tmp_name'][$index])){
    $_FILES['toupload']['name']     = $_FILES['image']['name'][$index];
    $_FILES['toupload']['type']     = $_FILES['image']['type'][$index];
    $_FILES['toupload']['error']     = $_FILES['image']['error'][$index];
    $_FILES['toupload']['size']     = $_FILES['image']['size'][$index];
    if ( $this->upload->do_upload('toupload')) {
        echo "Image uploaded!";
    }
}
Atal Prateek
  • 541
  • 3
  • 7