PHP 图像编码不起作用


PHP image encoding not working

我正在尝试在我的服务器上对.jpg文件进行编码,并将字符串发送回我的安卓应用程序。PHP 编码函数似乎不起作用 - 我总是得到空响应。

...
} else if ($tag == 'getPhoto') {
    $filePath = $_POST['filePath'];
    $filePath = mysql_real_escape_string($filePath);
    $photo_str = $db->getPhotoString($filePath);
    $photo_str = mysql_real_escape_string($photo_str);
    if (!empty($photo_str)) {
        echo $photo_str;
    } else {
        echo 'Something went wrong while retrieving the photo.';
    }
}
...
public function getPhotoString($filePath) {
    $type = pathinfo($filePath, PATHINFO_EXTENSION);
    $photoData = file_get_contents($filePath);
    $photo_str = 'data:image/' . $type . ';base64,' . base64_encode($photoData);
    // $photo_str = 'test_response';
    return $photo_str;
}

我知道我的文件路径是正确的,因为当我更改它时,我收到一个错误,说文件/目录不存在。而且,当我取消注释时,"test_response"有效。请帮忙!

更新 - 相关的安卓代码:

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(parameters));       
HttpResponse response = httpClient.execute(httpPost);
photoString = convertResponseToString(response);
Log.d("string response", photoString);
public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{          
    String res = "";
    StringBuffer buffer = new StringBuffer();
    is = response.getEntity().getContent();
    int contentLength = (int) response.getEntity().getContentLength(); //getting content length…..                   
    if (contentLength < 0) {
    } else {
           byte[] data = new byte[512];
           int len = 0;
           try {
               while (-1 != (len = is.read(data))) {
                   buffer.append(new String(data, 0, len)); //converting to string and appending  to stringbuffer…..
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
           try {
               is.close(); // closing the stream…..
           } catch (IOException e) {
               e.printStackTrace();
           }
           res = buffer.toString();     // converting stringbuffer to string…..
    }
    //System.out.println("Response => " +  EntityUtils.toString(response.getEntity()));
    return res;
}

这不是答案,只是一个建议,但我无法发表评论,因为我没有足够的声誉xD。无论如何,你有没有尝试过回声$photoData之后

$photoData = file_get_contents($filePath);

看看它是否装满了什么东西?也许你会得到一个空结果。另外,您能告诉我从 $_POST 变量中获取并传递给getPhotoString($filePath)函数时$filePath值是多少?我在想 mysql_real_escape_string() 有什么问题。