0

I have a form with multiple files upload, the action goes to a php file with the code:

if ($_FILES["filesToUpload"]["error"][0] != 4){

    $nome_array = $_FILES['filesToUpload']['name'];
    $tmp_nome_array = $_FILES['filesToUpload']['tmp_name'];
    $tipo_arquivo = $_FILES['filesToUpload']['type'];    
    $contagem_tmp_nome_array = count($tmp_nome_array);
    $nome_final = "atv";

    for ($i = 0; $i < $contagem_tmp_nome_array; $i++) {
        $extension = pathinfo($nome_array[$i], PATHINFO_EXTENSION);
        if ($extension == "gif" || $extension == "jpeg" || $extension == "jpg" || $extension == "png" || $extension == "pdf" || $extension == "txt" || $extension == "docx" || $extension == "doc" || $extension == "xls" || $extension == "xlsx") {
            $nome_array = $_FILES['filesToUpload']['name'];
            $tmp_nome_array = $_FILES['filesToUpload']['tmp_name'];
            $tipo_arquivo = $_FILES['filesToUpload']['type'];
            $contagem_tmp_nome_array = count($tmp_nome_array);
            $nome_final = "atv";

            if (move_uploaded_file($tmp_nome_array[$i], "../../uploads/".$nome_final."_".$idpedido."_".$i.".".$extension)) {
                echo "<script>console.log('Arquivo enviado: ".$nome_final."_".$idpedido."_".$i.".".$extension."');</script>";
                if ($i+1==$contagem_tmp_nome_array){
                    echo "<h1>Arquivos enviados com sucesso!</h1><br><a href='../../index.php'>Clique aqui para voltar.</a>";
                    header("Location: ../../atividadeEnviada.php");
                }
            } else {
                echo "<script>alert(\"Falha ao tentar processar arquivo ".$nome_array[$i]."\")</script>";
                echo "<script>history.go(-1);</script>";
                exit();
            }
        } else {
            echo "<script>alert(\"Formato de arquivo inválido!!\");";
            echo "history.go(-1);</script>";
            exit();
        }
    }
    header("Location: ../../atividadeEnviada.php");
}
header("Location: ../../atividadeEnviada.php");

But the header("Location: ../../atividadeEnviada.php"); part isn't redirecting, already tried even with javascript redirect, did I miss something? where did I go wrong?

AxerWeb
  • 37
  • 1
  • 8
  • please put form method = post in your form and button type = submit – VIKAS KATARIYA Oct 04 '19 at 13:07
  • please put your html code – VIKAS KATARIYA Oct 04 '19 at 13:08
  • 1
    _“But the header("Location: ../../atividadeEnviada.php"); part isn't redirecting”_ - with an `echo` statement right before it creating output, that should not really surprise you … – 04FS Oct 04 '19 at 13:10
  • It doesn't really make sense to even have an echo right before a redirect. All you're doing is echoing the message on the page you want to redirect the user from. How would they then even see the message? – M. Eriksson Oct 04 '19 at 13:28

1 Answers1

1

We cannot send the header after we start sending the output.

You can use output control for debug:

ob_start();
echo "blabla";
header("Location: ../../atividadeEnviada.php");
ob_end_flush();

But better just remove your echoes, as they make no sense on a server side.

freeek
  • 985
  • 7
  • 22
  • There's no reason to have an `echo` at all if they're just redirecting the user away from that page – M. Eriksson Oct 04 '19 at 13:20
  • @MagnusEriksson you are right, just giving the OP some hints, how really `echo` and `header` can be combined, but this makes no sense. Do you think this information is ambiguous? – freeek Oct 04 '19 at 13:25
  • @MagnusEriksson Sorry for the newbie question, that's the first form with upload I have done: I have to get off the echo from that part? if (move_uploaded_file($tmp_nome_array[$i], "../../uploads/".$nome_final."_".$idpedido."_".$i.".".$extension)) { header("Location: ../../atividadeEnviada.php"); } – AxerWeb Oct 04 '19 at 13:38
  • Even without echoes I got an blank page (no php errors) and no redirection done. – AxerWeb Oct 04 '19 at 13:44
  • @AlbertE. remove all the echoes and make some debug. You have `exit()` at some cases - this will quit the job without any redirection. – freeek Oct 04 '19 at 13:46
  • @freeek thanks for the replys, I will do it. – AxerWeb Oct 04 '19 at 13:56
  • @AlbertE. - Make sure you have error reporting turned on (use `E_ALL` while developing) and that you have enabled display errors. – M. Eriksson Oct 04 '19 at 14:13
  • Is there any other way to redirect without using the header () function? – AxerWeb Oct 04 '19 at 14:30
  • Using `javascript`. You can get some response (using ajax, sse. etc.), that file was uploaded and then redirect. Do you really need redirection? May be you can just show some message? – freeek Oct 04 '19 at 15:25
  • 1
    For being inexperienced I ended up leaving some echos out of this block which was disturbing the header function, now everything is working fine, I would like to thank everyone who helped me on this issue! – AxerWeb Oct 04 '19 at 16:47