Problem with sending headers

Asked

Viewed 417 times

0

Good people. This question should be the most asked but I’ve tried everything (I think I tried everything) but it still doesn’t give.

I have a php file here

<?php
                $ligacao = mysqli_connect("localhost", "root", "", "banco");
                if (mysqli_connect_errno()) {
                    echo "Erro na ligação MySQL: " . mysqli_connect_error();
                }
                if (isset($_POST["submit"])) {

                    $seccao = $_POST["seccao"];
                    $target_dir = "img/galeria/" . $seccao . "/";
                    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
                    $uploadOk = 1;
                    $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);

                    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
                    if ($check !== false) {
                        $uploadOk = 1;
                    } else {
                        echo "O ficheiro não é uma imagem.";
                        $uploadOk = 0;
                    }
                    if ($_FILES["fileToUpload"]["size"] > 500000) {
                        echo "Desculpa, o ficheiro é demasiado grande.";
                        $uploadOk = 0;
                    }
                    if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
                        echo "Apenas JPEG, JPG, PBG, GIF são permitidos";
                        $uploadOk = 0;
                    }
                    if ($uploadOk == 0) {
                        echo "O ficheiro não foi carregado.";
                    } else {
                        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                            echo "O ficheiro " . basename($_FILES["fileToUpload"]["name"]) . " foi submetido. <a href='index.php'>Clique aqui</a> para voltar para o inicio";
                        } else {
                            echo "Houve um erro com o upload do ficheiro.";
                        }
                    }
// colocar em variáveis os valores recebidos do formulário (método post) e que foram colocados no array associativo $_POST.
                    $nome = $_POST["nome"];
                    $descricao = $_POST["descricao"];
                    $foto = basename($_FILES["fileToUpload"]["name"]);

                    $sql = "insert into foto (nome, imagem, descricao, seccao) values ('$nome', '$foto', '$descricao', '$seccao')";

                    $resultado = mysqli_query($ligacao, $sql);
                    mysqli_close($ligacao);
                }
                ?>

Okay, I’ve been looking at some of the common errors that trigger this error like Excerpts and prints, html tags, whitespace, etc and I’ve tried to fix it but it keeps giving the same error. By the way, this is my mistake Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\projeto\inserirFotos.php:105) in C:\xampp\htdocs\projeto\inserirFotos.php on line 155 The 105 line is where the tag starts <?php and supposedly that’s where the outputs are supposed to be launched. I just can’t find the why of this happening!! Someone can figure this out for me?!

PS - the code shown here is without changes that I did to try to solve.

Thank you

  • 1

    PHP error - Cannot Modify header information There are a few more answers here, an alternative you didn’t talk about was to check the archive’s Find.

  • 2

    What is line 155?

  • 1

    if the script you posted starts counting on line 105 and has 48 lines, so, I’m missing more codes to get to line 155.

  • If you are using Sublime or another text editor that has the Save With Encoding option ... UTF8, do so. In English, Save with Encoding... UTF8. Sometimes it saves with UT8 with GOOD.

  • when directed to the page that would have to run the head, open the debugging console of your browser, see the code that it presents, if there is line break, comment or some code in the html displayed, theoretically would have to have only the line 1 empty.

1 answer

-1

At first the header will only work if it is the first sentence, there can be echo, text or anything else before it, the header must be the first PHP execution statement.

  • 1

    Only an addendum, a header (header) can be sent anywhere in the code, not necessarily in the first line first of all. What there can be is conflict between headers already sent.

  • http://forum.imasters.com.br/topic/168071-header-nao-funciona-nem-a-pau-redirecionamento/

  • http://answall.com/questions/4251/erro-do-php-cannot-modify-header-information

  • http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php

  • @Danielomine, I think what Michel meant to say is that the header cannot be modified after leaving the body, as echo or content before the tag php <?php. Now, I believe it’s okay to rewrite a header more than once, although I haven’t tested it yet.

  • 1

    Exactly, only one point that I find wrong is to say that it should be the first instruction of execution.. Incidentally, I did not deny the answer.. I don’t know who did.. Overwriting the header is possible as long as it hasn’t been fired. If you did, there is a way to revert by clearing the ob_clean(), flush() cache and equivalent functions.

  • when said sentence, I referred to what is interpreted by browser... vc can have 'n' lines like case, for, if, includes, what there can be before the header are instructions, even if it is explicit line break, echo, print, comments <!-- --> etc... if you open your browser where you say the header does not work, you will see that it displays some content.

Show 2 more comments

Browser other questions tagged

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