PHP does not recognize all Multiple input files

Asked

Viewed 121 times

0

Good morning, I’m doing a function to upload several images, but in PHP only reaches the last one. Here are my codes:

HTML

 <form id="msform" action="../controllers/controllerCadastraImagemProduto.php" method="POST" enctype="multipart/form-data">

 <input type='file' name="imagensPB[]" multiple>

 </form>

PHP

$i=0;
foreach ($_FILES["imagensPB"]["error"] as $key => $error) {
//Get the temp file path
$tmpFilePath = $_FILES['imagensPB']['tmp_name'][$i];

//Make sure we have a filepath
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "../img/produtos/" . $_FILES['imagensPB']['name'] [$i];

//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {


}
}
$i++;
}

Where can I be missing?

  • See the structure of the com array, print_r($_FILES);

  • Hello, I tried and this appeared: Array ( [imagensPB] => Array ( [name] => Array ( [0] => 2.jpg ) [type] => Array ( [0] => image/jpeg ) [tmp_name] => Array ( [0] => C: xampp tmp php8358.tmp ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 86060 ) ) )

  • you are storing the image (temporally named) in a variable string and not in an array, so every time the foreach goes through $tmpFilePath = $_FILES['imagensPB']['tmp_name'][$i]; he rewrites the variable...

  • Got it Rafael thanks. I’ll change the code.

1 answer

0

If you don’t find a solution you can try using Uploadify. Uploadify

Browser other questions tagged

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