Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android image file sent to php upload image file is application/octet-stream type and not image/jpeg?

Tags:

android

php

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 well except except the format of the image is application/octet-stream rather than image/jpeg that is sent from my android app? Is there anyway I can open the file application/octet-stream and change it to a image/jpeg type??

php file for upload...

$fileName = $_FILES['uploaded']['name'];

$tmpName = $_FILES['uploaded']['tmp_name'];

$fileSize = $_FILES['uploaded']['size'];

$fileType = $_FILES['uploaded']['type'];


$fp      = fopen($tmpName, 'rb');
$content = fread($fp, $fileSize);
$content = addslashes($content);

Works ok with a form upload form no problem there. But when used from the android app with the wrong image type it saves the blob file as empty 0, $fileSize shows the true size but when $content is created it shows nothing at all

177 rome 2.jpg image/jpeg 6876 [BLOB - 6.7 KiB] p (This is from a web page) 215 myImage.jpg application/octet-stream 1066 [BLOB - 0 B] (From phone)

Thx guys

like image 505
user1136994 Avatar asked Jan 08 '12 14:01

user1136994


3 Answers

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.

like image 52
hakre Avatar answered Oct 08 '22 14:10

hakre


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

like image 29
RaphaelDDL Avatar answered Oct 08 '22 14:10

RaphaelDDL


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());
        }
like image 1
Dev Avatar answered Oct 08 '22 14:10

Dev