how to stop the for loop in a given action?

Asked

Viewed 77 times

0

good people have a problem I have a form that uploads photos and saves the name in the database and moves the file to specified folder the problem this loop always repeats 5 times if I insert 5 photos everything happens normal photos are saved in the bank and moved for more folder if sending 1 photo of an error saying

Notice: Undefined offset: 2 in

More if sending 2 of an error

Notice: Undefined offset: 3 in

Plus if sending 3 of an error

Notice: Undefined offset: 4 in

More if sending 5 no error

plus photos are moved to folder and bank and saved name plus adds 5 lines for example if I send a photo only it is moved to folder more in bank and solvo 5 lines 4 are empty and one with the name of the photo 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

  • Make the modification I mentioned in my reply and you will not need to stop the loop. Your code is simply counting the number of elements in the wrong array.

  • I’ll try and get it fixed

1 answer

2


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

Change sizeof($Fotos) for sizeof($Fotos['name']). To avoid confusion with sizeOf() in other languages I suggest you use count() who does the same thing.

Answering the title question: You can stop a loop using a break; or even a simple return;

  • I did as you say and it worked right save my life

Browser other questions tagged

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