php - upload multi images to same field

Asked

Viewed 81 times

1

Goodnight,

I have a doubt and a question...

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

 Imagem_1 e Imagem_2.

But I don’t know how to separate the images, since it’s all from the same $_POST array['images']

Any hint?

2 answers

0


I believe that the issue is not the code itself, so I will focus only on logic.

When you go through the image variable with the foreach, you compare their position with the variable $key. Then in the if that compares to $key == 0, goes the code that makes the insertion in the database and moves to the folder the first image, and in the 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
  }
}
  • ok but example... I have given to insert also, I will not insert them 2x... how do I insert the data along with these images?

  • Can I use type... $_FILES['image'][0], and $_FILES['image'][1] ?

  • Make two Inserts, one for the images and the other for the data.

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 be something like this:

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

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

Browser other questions tagged

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