how to stop the for loop in a given action?

0

I have a problem with a form that uploads photos and saves the name in the database and moves the files to the specified folder. The problem is that the loop always repeats 5 times if I insert 5 photos. normal the photos are saved in the bank and moved to folder more if I send 1 photo of an error saying

  

Notice: Undefined offset: 2 in

More if 2 is sent an error

  

Notice: Undefined offset: 3 in

More if I send 3 an error

  

Notice: Undefined offset: 4 in

More if 5 send no error

plus the photos are moved to folder and in the bank and saved the name plus adds 5 lines for example if I send a photo only it and moved to folder more in the bank and solvo 5 lines 4 are empty and one with the name of the photo Which I sent

 <form  action="recebe_form.php" method="post" enctype="multipart/form-data">
     <label class="label">
        <span class="legend">fotos adicionais</span>        
            <input type="file" name="fotos[]" multiple/>
      </label>
 </form>

<?php 

 include_once("/../../../_app/config.inc.php");
 // Pasta de destino das fotos 

$Destino = "../../../../img_uploads/windows/1/"; 
 // Obtém dados do upload 
$Fotos = $_FILES["fotos"]; 
// Contagem de fotos enviadas 
$Conta = 0; 

 // Itera sobre as enviadas e processa as validações e upload 
for($i = 0; $i < sizeof($Fotos); $i++) 
{ 
 // Passa valores da iteração atual 
$Nome = $Fotos["name"][$i]; 
$Tamanho = $Fotos["size"][$i]; 
$Tipo = $Fotos["type"][$i]; 
$Tmpname = $Fotos["tmp_name"][$i]; 

// Verifica se tem arquivo enviado 
if($Tamanho > 0 && strlen($Nome) > 1) 
{ 
 // Verifica se é uma imagem 
if(preg_match("/^image\/(gif|jpeg|jpg|png)$/", $Tipo)) 
{ 
// Caminho completo de destino da foto 
$Caminho = $Destino . $Nome; 

 // Tudo OK! Move o upload! 
move_uploaded_file($Tmpname, $Caminho);

}
} $query = $pdo->query("INSERT INTO so_windows_gallery SET image ='$Nome', id_ = 1 "); 
}  

array (size=5)
  'name' => 
    array (size=1)
      0 => string '17172939023126.jpg' (length=18)
  'type' => 
    array (size=1)
      0 => string 'image/jpeg' (length=10)
  'tmp_name' => 
    array (size=1)
      0 => string 'C:\wamp64\tmp\phpBA42.tmp' (length=25)
  'error' => 
    array (size=1)
      0 => int 0
  'size' => 
    array (size=1)
      0 => int 194677
    
asked by anonymous 14.05.2017 / 22:11

1 answer

2

Use var_dump($Fotos); below the variable declaration and you'll see why your loop is not working. $ _FILES returns an associative array containing an indexed array for each association.

Change% of% by% with%. To avoid confusion with sizeof($Fotos) in other languages I suggest you use sizeof($Fotos['name']) which does the same thing.

Answering the title question: You can stop a loop using a sizeOf() or even a simple count()

    
14.05.2017 / 23:07