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);
– rray
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 ) ) )
– Ricardo Afonso
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...– RFL
Got it Rafael thanks. I’ll change the code.
– Ricardo Afonso