Fatal error: Call to Undefined Function http_response_code() php 5.2

Asked

Viewed 1,072 times

2

I took a script (Java android e php) from a book I’m studying, which simply takes a photo sent from android and saved in a folder on my server.
The android application ran without any error, but I ended up encountering an error in the php file:

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);
    }
}

Error happens in function http_response_code(400), searching the net I read something saying that this function only works in php 5.4 or higher (I’m not sure if this is true), so I was wondering if you have any alternative function for my version of php 5.2.17.
For those who did not understand what I’m trying to do, I’m sending a photo by 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 already exists a file with the same name returns 501, and if it occurs all right returns 200).

2 answers

2


Change the use of the function http_response_code() by function header(), by typing 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.

About 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.

  • I gave you one more point of useful response because it worked once and now there are absurd things happening here.

  • On my server I have two php files, upload_foto.php and webservicesistema.php. The first code is just above and the second has only a mysql command that returns a number according to the query.

  • For example if the user exists returns 1, if there is error returns 2, and if everything is ok it returns the user data in json format.

  • and how this correlates with http status?

  • I am using Chrome’s Postman plugin for testing (I’ve been using it for a while) and when I send a photo to that upload_foto.php it gives the following error: Warning: Cannot Modify header information - headers already sent by (output Started at upload_foto.php:1) in upload_foto.php on line 22

  • line 22: header('HTTP/1.0 200');

  • And when I send the photo to the webservicesistema.php file it saves the normal photo in the server folder!!!!!

  • then you already have a new question. Don’t ask multiple questions in one. About the header error, the error message itself already says the problem.

  • Sorry I got confused by this situation

  • But going back to the question then there is no way to change the http code in the header? Your contribution worked once and then the second time it appeared this

Show 5 more comments

0

Following the function header() by @Daniel Omine I was able to make it work once the photo upload, 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 stack @Bruno Augusto, who translated from a friend of his from another forum.
Looking response from him (that you can take a look here Error - Cannot Modify header information) I looked for one of the bugs in my code, which was to see if there was any whitespace before the opening tag ?php and had not, but searching a little more I found something about some 'invisible characters' that can be generated when saving the files in some editors, and looking for how to remove these characters I found the HXD program that can be downloaded from the link http://mh-nexus.de/downloads/HxDSetupPTB.zip .
The program is very light and simple to use, install it and open the file that is giving error, its screen will look like this:

inserir a descrição da imagem aqui

This image shows that before the opening tag ?php there are other characters (in my case were other different characters) that do not appear in the normal editors, Oce just need to remove them and save your file, simple like this.
But how do I know that’s my mistake?
Simple, in my case the browser said that the error occurred on line 22. But it wasn’t really that line that gave the error, that line just tried to perform the function Header(), the actual line of the error is being shown in the parentheses part (output started at .../upload_foto.php:1) in the case on line 1, but on that line only had the opening tag ?php, then that’s when I realized there had to be something 'hidden' there.

Browser other questions tagged

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