php - upload multi images to same field

1

Good Night,

I have a question and an issue ...

I want to upload multiple images, but in my database I have the 2 fields:

 Imagem_1 e Imagem_2.

But I do not know how to separate the images, since everything is from the same array $ _POST ['images']

Any tips?

    
asked by anonymous 01.04.2018 / 00:58

2 answers

0

I think the question is not the code itself, so I'm going to focus only on logic.

When you go through the image variable with foreach , you compare their position with the $key variable. Then in the if that compares if $key == 0 , goes the code that inserts in the bank and moves to the folder the first image, and in else , will do the same with the second image

foreach($_FILES['imagem'] as $key => $imagem) {
  if($key == 0) {
    // Codigo que vai salvar na coluna imagem_1 e mover para a pasta
  } else {
    // Codigo que vai salvar na coluna imagem_2 e mover para a pasta
  }
}
    
01.04.2018 / 01:10
0

You can create an input with the name attribute as an array:

<form action='' method='post'>
    <input name='imgs[]' type='file' multiple>
</form>

And in PHP:

var_dump($_POST["imgs"]);

The result will look something like this:

array(2) { [0]=> string(8) "img1.png" [1]=> string(8) "img2.png" }

So just loop and convert the images to the blob type, move to a folder or just save the file name

    
01.04.2018 / 01:18