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

前端 未结 4 1636
不思量自难忘°
不思量自难忘° 2020-12-09 22:38

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

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-09 23:19

    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

提交回复
热议问题