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!

7
  • Just loop over the variable $_POST['image-to-upload']. It's an array, so you can go over all the items in it. Commented Dec 26, 2016 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 Commented Dec 26, 2016 at 14:40
  • The search for "php loop through array" in google gives this as a first result: php.net/manual/en/control-structures.foreach.php Commented Dec 26, 2016 at 14:42
  • And you can also check the Documentation here in StackOverflow: stackoverflow.com/documentation/php/2213/… Commented Dec 26, 2016 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 :/ Commented Dec 26, 2016 at 14:53

3 Answers 3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Did you read the question? It's not related at all to multiple files uploading
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.