如何将 base64 图像发送到服务器 - PHP
How to send base64 image to server - PHP
我正在通过 PHP 文件从 Android 应用向服务器发送 base64 字符串。
我从教程视频中得到了这个:
<?php
require 'Init.php';
header('Content-type : bitmap; charset=utf8');
if(isset($_POST['encoded_string'])){
$encoded_string = $_POST['encoded_string'];
$image_name = $_POST['image_name'];
$decoded_string = base64_decode($encoded_string);
$path = 'ProfileIcons/' .$image_name;
$file = fopen($path, 'wb');
$is_written = fwrite($file, $decoded_string);
fclose($file);
}
?>
目录里存的是图片,打开的时候没有图片。这是一个空白的png。代码有问题吗?如果是这样,我应该使用什么?谢谢
有几点你应该检查:
1.Make确定服务器已经收到base64字符串
$encoded_string = $_POST['encoded_string'];
检查 $encoded_string
的长度,它应该与 android 客户所说的长度相同。
- 确保解码正常
$decoded_string = base64_decode($encoded_string);
检查 $decoded_string
的长度,它不应为零或奇数。
- 确保写入的文件有效
$is_written = fwrite($file, $decoded_string);
$is_written
应该是已经写入文件的数据长度,如果是false
,那就有问题了。
我正在通过 PHP 文件从 Android 应用向服务器发送 base64 字符串。
我从教程视频中得到了这个:
<?php
require 'Init.php';
header('Content-type : bitmap; charset=utf8');
if(isset($_POST['encoded_string'])){
$encoded_string = $_POST['encoded_string'];
$image_name = $_POST['image_name'];
$decoded_string = base64_decode($encoded_string);
$path = 'ProfileIcons/' .$image_name;
$file = fopen($path, 'wb');
$is_written = fwrite($file, $decoded_string);
fclose($file);
}
?>
目录里存的是图片,打开的时候没有图片。这是一个空白的png。代码有问题吗?如果是这样,我应该使用什么?谢谢
有几点你应该检查:
1.Make确定服务器已经收到base64字符串
$encoded_string = $_POST['encoded_string'];
检查 $encoded_string
的长度,它应该与 android 客户所说的长度相同。
- 确保解码正常
$decoded_string = base64_decode($encoded_string);
检查 $decoded_string
的长度,它不应为零或奇数。
- 确保写入的文件有效
$is_written = fwrite($file, $decoded_string);
$is_written
应该是已经写入文件的数据长度,如果是false
,那就有问题了。