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