doumuyu0837 2015-05-24 13:53
浏览 709
已采纳

尝试上传图像时,base64_decode返回null

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:

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

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

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制