【问题标题】:base64_decode return null when trying to upload image尝试上传图片时 base64_decode 返回 null
【发布时间】:2015-05-24 13:53:51
【问题描述】:

我正在尝试将图像上传到服务器,但 base64_decode 总是返回 null 我从 SO 中阅读了很多不同的案例,但没有任何效果。 这是我发出 http 请求之前的 java 代码:

      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代码:

                     $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);

我也试过$file = fopen($imagepath, 'wb'); 仍然$binary 总是返回null; 有人可以帮助我了解问题所在吗?

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

【问题讨论】:

    标签: java php base64 decode


    【解决方案1】:

    所以我想通了: 1.您需要保存图像的路径必须相对于服务器中的Web服务(在我的情况下为php代码),这也意味着您必须将图像保存在同一服务器上。 2. file_put_contents 使用起来更方便,将fopen、fwrite 和fclose 一起替换,省去了麻烦。 希望对大家有帮助

    【讨论】:

      猜你喜欢
      • 2016-05-11
      • 2021-08-12
      • 2021-12-31
      • 2019-09-21
      • 2020-09-26
      • 1970-01-01
      • 2020-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多