Help with foreach error

Asked

Viewed 206 times

0

I’m developing a system that needs up 2 types of img a small and a large for the database but when I send up 3 img small and 3 img great test my foreach does not go up all the img small or their respective it rises only the 3 img Big he even climbs 3 imgsmall but he repeats the first 3 times someone can help me?

Code:

require"conexao.php";

    if(isset($_POST['port'])){  
    //dados que serão incluidos
    $nome = $_POST['galeria'];


foreach($_FILES['imgp']['tmp_name'] as $key => $name_temp){
        $name = $_FILES['imgp']['name'] [$key];
        $tmpnm = $_FILES['imgp']['tmp_name'][$key];
        $type = $_FILES['imgp']['type'][$key];
        $size = $_FILES['imgp']['size'][$key];

        $dir = "../images/small/portfolio/".$name;
        $mover = move_uploaded_file($tmpnm , $dir);

        foreach($_FILES['imgb']['tmp_name'] as $k => $n){
                $nomev = $_FILES['imgb']['name'] [$k];
                $t = $_FILES['imgb'] ['tmp_name'] [$k];
                $tipo = $_FILES['imgb']['type'][$k];
                $tam = $_FILES['imgb']['size'][$k];


        $dire = "../images/big/portfolio/".$nomev;
        $move = move_uploaded_file($t , $dire);


    if($mover&&$move){
             $sql = mysqli_query($mysqli, "INSERT INTO portfolio(nome, img, type, size, img_big, tipo, tam) 
                values('$nome', '$name', '$type', '$size', '$nomev', '$tipo', '$tam')") or die (mysqli_error());
            if($sql){
                echo "foi";
            }
            else{
                echo "erro database";
            }
        }else{
            echo"<h1>Ocorreu um erro.</h1>";
        }
      }
   }
}
  • The problem would be that the big pictures are repeating?

  • the little ones are repeating the big ones so going right

  • if I climb 3 small and 3 large he will catch the 1 small q climbed and will repeat her 3x. but the big he will climb normal

  • Probably your problem is the foreach of the large images inside the foreach of the small images, it should repeat all large images for each small image. I can’t see the small image being repeated from here, maybe if you run a few more tests and post more information maybe it will be easier to help you.

  • Right what you said I’ve used the var_dump() to see the results of the first foreach he doesn’t pass by all he always stops at the first img already the second takes everything normal. what I must do for them to work the way he climbs all the img small and all the img great?

  • I put a response with a code that should approximate what you need, test it and report the result later.

Show 1 more comment

1 answer

0


In this case is being used a foreach of the large images inside the small images, try to use a foreach only.

The example below may not work 100% because it considers that large images have the same index as small

if(isset($_POST['port'])){
//dados que serão incluidos
    $nome = $_POST['galeria'];


    foreach($_FILES['imgp']['tmp_name'] as $key => $name_temp){
        $name = $_FILES['imgp']['name'] [$key];
        $tmpnm = $_FILES['imgp']['tmp_name'][$key];
        $type = $_FILES['imgp']['type'][$key];
        $size = $_FILES['imgp']['size'][$key];

        $dir = "../images/small/portfolio/".$name;
        $mover = move_uploaded_file($tmpnm , $dir);

        $nomev = $_FILES['imgb']['name'] [$key];
        $t = $_FILES['imgb'] ['tmp_name'] [$key];
        $tipo = $_FILES['imgb']['type'][$key];
        $tam = $_FILES['imgb']['size'][$key];


        $dire = "../images/big/portfolio/".$nomev;
        $move = move_uploaded_file($t , $dire);


        if($mover&&$move){
            $sql = mysqli_query($mysqli, "INSERT INTO portfolio(nome, img, type, size, img_big, tipo, tam)
values('$nome', '$name', '$type', '$size', '$nomev', '$tipo', '$tam')") or die (mysqli_error());
            if($sql){
                echo "foi";
            }
            else{
                echo "erro database";
            }
        }else{
            echo"<h1>Ocorreu um erro.</h1>";
        }
    }
}

  • everything was solved with a foreach intao I tried to do but I had done different guess why gave error I passed the 2 move_uploaded_file at the end of foreach I guess that’s why it went wrong

  • If the move_uploaded_file were after the condition of the provable sql that would give error, apart from that I see nothing "out of the ordinary". Another detail is that even this code that I passed you is not 100% proof of errors, but it is nothing that more checking and exception handling tests do not solve.

Browser other questions tagged

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