0

I am writing an app that will allow the user to take a picture, which is automatically sent to an HTTP server to be classified via HTTP POST multipart/form-data. To build and send these requests, I am using Apache's HTTPClient library. I am somewhat confident that my Java clientside code works, as I have tested with one of the Imgur API endpoints and the images uploaded correctly; therefore, I think it is most likely that there is an error in the endpoint on my server. I am sending the request to server to be received by a PHP script that I wrote to receive the file, save it to directory on the server, and send a response back to the client appropriately. When I test this, the server always sends back a response with status code 200, even though the script obviously is not working. I have tried manually setting the response status code to 500 to force an error code, and even that doesn't work, which makes me think that the entire script does not even run when the request hits the endpoint (if it even hits the endpoint). I expect this to be either an error in server configuration, or an error in the script itself. However, I have pasted the Java code just in case. You may find some errors there too. I am not great with networking or with PHP, so I could definitely use some more experienced eyes.

  • Note: I know my PHP code looks awful. You will probably find a lot of bad practices; sorry.

My server code:

<?php

$path = '/home/user01/Documents/uploaded_images/';
$ext = '.jpg';

function getFileName() {
  return 'image_'.date('Y-m-d_His');
}

//Receive the data from android client

$file = $_FILES['uploadedFile'];


//process the data

$filename = $path . getFileName() . '_' . $_SERVER['REMOTE_ADDR'] . $ext;

if (!is_uploaded_file($file['tmp_name']) ||
    !copy($file['tmp_name'], $filename))
{
  $message = 'Could not save file as' . $filename;
  $error = True;
}
else {
  $message = "Image uploaded successfully!";
  $error = False;
}

//return response to the server

if ($error) {
  http_response_code(500);
}

echo json_encode(
  array(
    'status' => 'Repsonse Code :' . http_response_code(),
    'message' => $message
  ), JSON_FORCE_OBJECT);


?>
oaky_afterbirth
  • 125
  • 3
  • 15
  • 1
    You need to figure out if this is a client or server problem, then remove the code and tags that aren't relevant. – miken32 Nov 14 '18 at 21:42
  • @miken32 I removed the Android tag. I _expect_ that it is a server problem, but I am not 100% sure. Therefore, I am hesitant to remove Java code that could be the problem. – oaky_afterbirth Nov 14 '18 at 21:48
  • 1
    So make up a quick HTML form, open it up in a web browser, and try uploading a file. What happens? – miken32 Nov 14 '18 at 21:49
  • 1
    If you suspect that the PHP code isn't even executed, there isn't much we can do by looking at it. Add a `die('foo');` in the top of your PHP code and check if the app gets that response. If yes, then the app hits the endpoint and it's a code issue. If no, then there's a request or server issue. – M. Eriksson Nov 14 '18 at 21:49
  • @miken32 I added an HTML form for uploading an image, and when I submit the request through the form, nothing happens, to my knowledge. The page just redirects to the upload.php page and loads normally. – oaky_afterbirth Nov 14 '18 at 22:06
  • If you're not seeing your expected output, I suggest you look here first: https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – miken32 Nov 14 '18 at 22:08
  • @MagnusEriksson When I add the die('foo') to the beginning of the script, nothing happens. However, this is the case even when I send the request from a form in index.html and set the request to be sent to the upload.php script. So the script seems to not be working even when the request is sent locally. – oaky_afterbirth Nov 14 '18 at 22:09
  • After trying the aforementioned html form idea and adding the aforementioned die() function, I discovered with certainty that the script was not running, and eventually traced it back to a server configuration problem. Thank you all for the help! – oaky_afterbirth Nov 14 '18 at 22:58

1 Answers1

0

Update and Resolution:

After some troubleshooting with the help of a few of the post commenters, I first tried writing a simple HTML form to submit POST requests locally to the PHP script, and after those requests failed, I then added the die() function at the beginning of the code, and still, nothing happened. This allowed me to figure out that the error.

The problems that I was having were caused by a bad server configuration. It was causing PHP to not be recognized as an installed language; therefore, the script was not being executed at all. Once the configuration was fixed, the script worked exactly as expected.

Much kudos to the commenters for their insight.

oaky_afterbirth
  • 125
  • 3
  • 15