Fatal error: Call to undefined function http_response_code () php 5.2 [duplicate]

2

I got a script (Java android and php) from a book I'm studying that simply takes a photo sent from android and saves it to a folder on my server. Home The android application ran without any error, but I ended up with an error in the file in php:

if ($_FILES["arquivo"]["error"] > 0) {
    // Bad Request
    http_response_code(400);
} else {
    $arquivo_destino = "imagens/" . $_POST["titulo"] . ".jpg";
    if (file_exists($arquivo_destino)) {
        http_response_code(501);
    } else {
      move_uploaded_file(
          $_FILES["arquivo"]["tmp_name"],
          $arquivo_destino
      );
      http_response_code(200);
    }
}

The error happens in the http_response_code(400) function, searching the net li something saying that this function only works in php 5.4 or higher (not sure if this is true), so I wanted to know if it has any alternative function for my version of php 5.2 .17. Home For those who did not understand what I'm trying to do, I'm sending a photo through the android and returning an http code according to what happens there on the server in the php file (if there is an error with the file returns 400, if there is already a file with the same name returns 501, and if all else happens, 200 returns.)

    
asked by anonymous 05.04.2016 / 14:54

2 answers

2

Switch the header () function link by writing the headers directly:

if ($_FILES["arquivo"]["error"] > 0) {
    // Bad Request
    //http_response_code(400);
    header('HTTP/1.0 400 Page not found');
} else {
    $arquivo_destino = "imagens/" . $_POST["titulo"] . ".jpg";

    if (file_exists($arquivo_destino)) {
        //http_response_code(501);
        header('HTTP/1.0 500');
    }
    else {
        move_uploaded_file(
            $_FILES["arquivo"]["tmp_name"],
            $arquivo_destino
        );
        //http_response_code(200);
        header('HTTP/1.0 200');
    }
}

For any PHP function, when you need to understand the function better, just type in your browser: http://php.net/NOME-DA-FUNÇÃO . Example: http://php.net/http_response_code .

Regarding the code you posted, I will avoid discussing whether what you are doing is right or wrong. But I recommend not doing this. Anyway, do as you see fit.

    
05.04.2016 / 15:17
0

Following the header() function of @Daniel Omine I was able to make the photo upload work once, but then I came across the error:

Warning: Cannot modify header information - headers already sent by (output started at .../upload_foto.php:1) in .../upload_foto.php

So searching I found an answer here in the @Bruno Augusto stack, which he translated from a friend of his from another forum. Looking at it (you can take a look here) Error - Can not modify header information ) I looked for one of the errors in my code, which was to see if there was no white space before the opening tag ?php and I did not, but searching a little more I found something about some 'invisible characters' that can be generated save the files to some editors, and looking for how to remove those characters I found the HXD program that can be downloaded from the link link . The program is very light and simple to use, install it and open the file that is giving error, his screen will look like this:

In this image of the to see that before the opening tag ?php there are other characters (in my case were different characters) that do not appear in the normal editors, you just need to remove them and save your file, simple as well. > But how do I know if this is my mistake?
Simple, in my case the browser said that the error occurred on line 22. But it was not really that line that gave the error, that line only tried to execute the function Header() , the actual line of error is being shown in the part between parentheses (output started at .../upload_foto.php:1) in the case on line 1, but in this line only had the opening tag ?php , so that's when I realized I had to have something 'hidden' there.     

05.04.2016 / 17:22