base64_decode return 尝试上传图片时为空
base64_decode return null when trying to upload image
我正在尝试将图像上传到服务器,但 base64_decode
总是 return 为 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
总是 return 和 null
;
有人可以帮我理解问题是什么吗?
我也检查了 $base
看起来没问题,我也尝试删除 \n
或 \
但没有任何帮助
所以我弄明白了:
1. 您需要保存图像的路径必须与服务器中的 Web 服务(php 代码在我的例子中)相关,这也意味着您必须将图像保存在同一台服务器上。
2. file_put_contents 更容易使用,将 fopen、fwrite 和 fclose 一起替换,省去麻烦。
希望对大家有所帮助
我正在尝试将图像上传到服务器,但 base64_decode
总是 return 为 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
总是 return 和 null
;
有人可以帮我理解问题是什么吗?
我也检查了 $base
看起来没问题,我也尝试删除 \n
或 \
但没有任何帮助
所以我弄明白了: 1. 您需要保存图像的路径必须与服务器中的 Web 服务(php 代码在我的例子中)相关,这也意味着您必须将图像保存在同一台服务器上。 2. file_put_contents 更容易使用,将 fopen、fwrite 和 fclose 一起替换,省去麻烦。 希望对大家有所帮助