Hello all hope someone can can help- To fininsh my android app I just need to finish the class were the user can upload there image to the server(MySql).
All works w
Some androids indeed upload using mime-type application/octet-stream
instead of the correct one.
For that, i've made the following:
//the uploaded file
$img = $_FILES['img'];
//array for type if octet
$realMime = array(
'useNew' => false,
'type' => ''
);
if($img['type'] == "application/octet-stream"){
$imageMime = getimagesize($img['tmp_name']); // get temporary file REAL info
$realMime['type'] = $imageMime['mime']; //set in our array the correct mime
$realMime['useNew'] = true; //set to true for next if
}
//now check if we will use the realMime or the one sent by browser
//$mimeCheck is the things you want to check. In my case i just wanted PNG or JPG
if($realMime['useNew'] == true){ //if true, use realMime to check
$mimeCheck = ($realMime['type'] != "image/png" && $realMime['type'] != "image/jpeg" && $realMime['type'] != "image/jpg");
}else{ //if not using real mime, go with the one browser sent us
$mimeCheck = ($img['type'] != "image/png" && $img['type'] != "image/jpeg" && $img['type'] != "image/jpg");
}
//returns error if image not png/jpg/jpeg
if($mimeCheck){
//return some error
}
With this you can check. Of course have to make an error handler for getimagesize()
since user can upload an octet-stream file that isn't really an image.
For more info, read PHP.net getimagesize() doc
At Android side to make proper call we should make the web-service call like this:
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("https://someserver.com/api/path/");
postRequest.addHeader("Authorization",authHeader);
//don't set the content type here
//postRequest.addHeader("Content-Type","multipart/form-data");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
File file = new File(filePath);
FileInputStream fileInputStream = new FileInputStream(file);
reqEntity.addPart("parm-name", new InputStreamBody(fileInputStream,"image/jpeg","file_name.jpg"));
postRequest.setEntity(reqEntity);
HttpResponse response = httpclient.execute(postRequest);
}catch(Exception e) {
Log.e("URISyntaxException", e.toString());
}
application/octet-stream
or image/jpeg
is only the mime-type the HTTP client (browser) has added as additional information next to the binary file-data itself.
So the mime-type shouldn't be of any issue here. It's just that you see a difference between the requests for the case your program fails, but this must not be the cause of your issue.
So you're basically looking at the wrong place.
Instead you should add error and condition checking to your code. Especially precondition checks so that you know that your script can properly operate on the data it gets provided.
From what you have made visible in your question, there is nothing more specifically I can add. Hope this is helpful anyway.
Also the PHP Manual has a section about file-uploads which contains some useful information incl. dealing with error-cases.
Next to that your code might just have a problem with how you insert the data into your database, so the problem might not be related at all with the file-upload technically.
I know its a bit late but I think this answer can help other peoples also.
I have found this on: http://php.net/manual/en/features.file-upload.php
// DO NOT TRUST $_FILES['upfile']['mime'] VALUE !!
// Check MIME Type by yourself.
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search(
$finfo->file($_FILES['upfile']['tmp_name']),
array(
'jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
),
true
)) {
throw new RuntimeException('Invalid file format.');
}
So if you want to check for mime type you should go for finfo
not the mime type given by $_FILE[type]
. It can be wrong so guess the mime type on your own. Specially when devices are different like in this case android.