2015-05-24 13:53
浏览 594


I am trying to upload an image to server but base64_decode always returns null i read a lot of different cases from SO but nothing works. here is my java code before i make my http request:

      ByteArrayOutputStream stream = new ByteArrayOutputStream();
            // Must compress the Image to reduce image size to make upload easy
            bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream); 
            byte[] byte_arr = stream.toByteArray();
            // Encode Image to String
            encodedString = Base64.encodeToString(byte_arr,Base64.NO_WRAP);
            params.add(new BasicNameValuePair(TAG_IMAGE, encodedString));
           ...and the http request

php code:

                          //$response["base"] = $base;
                          $response["binary"] = $binary;
                         header('Content-Type: bitmap; charset=utf-8'); 
                         $imagepath='the path of the image';
                            $file = fopen($imagepath, 'w');
                            fwrite($file, $binary);

i also tried $file = fopen($imagepath, 'wb'); still $binary is always return with null; somebody can please help me understand what is the problem?

also i checked $base and it looks ok, i also tried to remove or \\ but nothing helps

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将图像上传到服务器,但 base64_decode 总是返回null i读取 来自SO的很多不同的案例但没有任何作用。\在我发出我的http请求之前我的java代码:

  ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
 // 必须压缩图像以减小图像大小才能轻松上传
 byte [] byte_arr = stream.toByteArray(); 
 encodedString = Base64.encodeToString(byte_arr,Base64.NO_WRAP); 
 \ params.add(new BasicNameValuePair(TAG_IMAGE,encodedString))  ; 


  $ base = $ _ REQUEST [  'image']; 
 // $ response [“base”] = $ base; 
 $ binary = base64_decode($ base,TRUE); 
 $ response [“binary”] = $ binary; 
 header(  'Content-Type:bitmap; charset = utf-8');  
 $ imagepath ='图像的路径'; 
 $ file = fopen($ imagepath,'w'); 
 fwrite($ file,$ binary); 
 fclose($ file); 
 <  / code>  

我也试过 $ file = fopen($ imagepath,'wb'); 仍然 $ binary 总是返回 用 null ; somebody可以帮我理解是什么问题?

我也检查了 $ base ,看起来没问题, 我还尝试删除 \\ 但没有任何帮助

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqvtm82066 2015-05-31 11:00

    so i figure it out: 1. the path that you need to save the image must be relative to the web service (php code in my case) in the server which also mean you have to save the image at the same server. 2. file_put_contents is a lot easyier to use and replace the fopen, fwrite and fclose together and save you trubble. i hope it will help you guys

    解决 无用
    打赏 举报

相关推荐 更多相似问题