0

I have multiple input fields with the same name. they look like:

<input type="hidden" class="image-hidden" name="image-to-upload[]" />
<input type="hidden" class="image-hidden" name="image-to-upload[]" />
<input type="hidden" class="image-hidden" name="image-to-upload[]" />
<input type="hidden" class="image-hidden" name="image-to-upload[]" />
...
...

I am uploading with this code:

<?php
    if(isset($_POST['new-blogpost'])) {
        $img = $_POST['image-to-upload'][0];
        $img = str_replace('data:image/jpeg;base64,', '', $img);
        $img = str_replace(' ', '+', $img);
        $data = base64_decode($img);
        $file = 'image.jpg';
        $success = file_put_contents($file, $data);
    };
?>

the problem is, this code will only upload the first input fields picture.

How do I have to rewrite my code to upload all input fields? (I know that I have to give my files unique names in that case, but thats no my question. I'm wondering how to tell PHP it has to loop through all the input fields and do the upload.

Thanks in advance!

maxischl
  • 579
  • 1
  • 11
  • 29
  • Just loop over the variable `$_POST['image-to-upload']`. It's an array, so you can go over all the items in it. – Dekel Dec 26 '16 at 14:38
  • well yes, thanks. but i don't know how to do that :/ tried to figure it out but i don't really understand – maxischl Dec 26 '16 at 14:40
  • The search for "php loop through array" in google gives this as a first result: http://php.net/manual/en/control-structures.foreach.php – Dekel Dec 26 '16 at 14:42
  • And you can also check the Documentation here in StackOverflow: http://stackoverflow.com/documentation/php/2213/loops#t=201612261442355323506 – Dekel Dec 26 '16 at 14:42
  • true, i read this, but if i use foreach ($_POST['image-to-upload']) {} i don't know what i have to use as $value. I simply do not understand :/ – maxischl Dec 26 '16 at 14:53
  • Did you check the answer? – Dekel Dec 26 '16 at 15:05
  • yes, i did and thank you very much, that is pretty amazing, and the comparison was really good to get some understanding :) – maxischl Dec 26 '16 at 15:45

3 Answers3

1

Use a foreach loop:

$list = ['apple', 'banana', 'cherry'];

foreach ($list as $value) {
    if ($value == 'banana') {
        continue;
    }
    echo "I love to eat {$value} pie.".PHP_EOL;
}

In your example - your array's name is $_POST['image-to-upload'] so you can loop over it:

foreach($_POST['image-to-upload'] as $img) {
    $img = str_replace('data:image/jpeg;base64,', '', $img);
    $img = str_replace(' ', '+', $img);
    $data = base64_decode($img);
    // $file = 'image.jpg'; // here you need to create the unique filename
    $success = file_put_contents($file, $data);
}
miken32
  • 42,008
  • 16
  • 111
  • 154
Dekel
  • 60,707
  • 10
  • 101
  • 129
0

For iterating all the files use foreach loop

foreach($_FILES['image-to-upload']['tmp_name'] as $key => $tmp_name)
    {

      //Code

    }

See this link for more understanding:

PHP Multiple File Array

Community
  • 1
  • 1
Ambrish Pathak
  • 3,813
  • 2
  • 15
  • 30
0

Declare an array and equate it to your post data like $arr =new array(); $arr = $_POST["img[]"]; and with a for loop you can loop through your array

Yusif
  • 31
  • 4