如何从 PHP 中的字节 [] 获取 image_name 和 image_tmp_name?
How do I get the image_name and image_tmp_name from a byte [] in PHP?
我目前正在从我的前端向我的域和 PHP 代码发送一个字节 []。我要发送的字节数组名为 "photo",它的发送方式类似于 System.Text.Encoding.UTF8,"application/json"。我的目标是将字节 [] 放入图像中,然后将其上传到我的域文件夹(已经存在)我是否需要从字节 [] 中获取 image_name 和 image_tmp_name 才能执行这个?我有点不确定我应该如何让它工作。
我目前有这个代码:
<?php
$value = json_decode(file_get_contents('php://input'));
if(!empty($value)) {
print_r($value);
}
?>
使用此代码,打印会给我一大行包含字节 [] 的文本。
我现在如何从这个字节 [] 中获取 image_name 和 image_tmp_name?我的目标是使用如下所示的代码将图像上传到我的域地图(名为 photoFolder 已经存在):
$image_tmp_name = ""; //I currently do not have this value
$image_name = ""; //I currently do not have this value
if(move_uploaded_file($image_tmp_name, "photoFolder/$image_name")) {
echo "image successfully uploaded";
}
如何发送:
static public async Task <bool> createPhotoThree (byte [] imgData)
{
var httpClientRequest = new HttpClient ();
var postData = new Dictionary <string, object> ();
postData.Add ("photo", imgData);
var jsonRequest = JsonConvert.SerializeObject(postData);
HttpContent content = new StringContent(jsonRequest, System.Text.Encoding.UTF8, "application/json");
var result = await httpClientRequest.PostAsync("http://myadress.com/test.php", content);
var resultString = await result.Content.ReadAsStringAsync ();
return true;
}
这是解决方案。像这样更改 C#
方法:
static public async Task<bool> createPhotoThree(string imgName, byte[] imgData) {
var httpClientRequest = new HttpClient();
var postData = new Dictionary<string, object>();
postData.Add("photo_name", imgName);
postData.Add("photo_data", imgData);
var jsonRequest = JsonConvert.SerializeObject(postData);
HttpContent content = new StringContent(jsonRequest, System.Text.Encoding.UTF8, "application/json");
var result = await httpClientRequest.PostAsync("http://myadress.com/test.php", content);
var resultString = await result.Content.ReadAsStringAsync();
return true;
}
你 php
代码如下:
$input = file_get_contents('php://input');
$value = json_decode($input, true);
if (!empty($value) && !empty($value['photo_data']) && !empty($value['photo_name'])) {
file_put_contents($value['photo_name'], base64_decode($value['photo_data']));
}
你看,当你调用 JsonConvert.SerializeObject(postData)
时,你的 byte[]
变成了一个 base64 编码的字符串。您正在 POST-body 中发送该数据目录。所以在 php
端你需要先 json_decode()
of php://input
然后 base64_decode()
of image bytes.
我目前正在从我的前端向我的域和 PHP 代码发送一个字节 []。我要发送的字节数组名为 "photo",它的发送方式类似于 System.Text.Encoding.UTF8,"application/json"。我的目标是将字节 [] 放入图像中,然后将其上传到我的域文件夹(已经存在)我是否需要从字节 [] 中获取 image_name 和 image_tmp_name 才能执行这个?我有点不确定我应该如何让它工作。
我目前有这个代码:
<?php
$value = json_decode(file_get_contents('php://input'));
if(!empty($value)) {
print_r($value);
}
?>
使用此代码,打印会给我一大行包含字节 [] 的文本。
我现在如何从这个字节 [] 中获取 image_name 和 image_tmp_name?我的目标是使用如下所示的代码将图像上传到我的域地图(名为 photoFolder 已经存在):
$image_tmp_name = ""; //I currently do not have this value
$image_name = ""; //I currently do not have this value
if(move_uploaded_file($image_tmp_name, "photoFolder/$image_name")) {
echo "image successfully uploaded";
}
如何发送:
static public async Task <bool> createPhotoThree (byte [] imgData)
{
var httpClientRequest = new HttpClient ();
var postData = new Dictionary <string, object> ();
postData.Add ("photo", imgData);
var jsonRequest = JsonConvert.SerializeObject(postData);
HttpContent content = new StringContent(jsonRequest, System.Text.Encoding.UTF8, "application/json");
var result = await httpClientRequest.PostAsync("http://myadress.com/test.php", content);
var resultString = await result.Content.ReadAsStringAsync ();
return true;
}
这是解决方案。像这样更改 C#
方法:
static public async Task<bool> createPhotoThree(string imgName, byte[] imgData) {
var httpClientRequest = new HttpClient();
var postData = new Dictionary<string, object>();
postData.Add("photo_name", imgName);
postData.Add("photo_data", imgData);
var jsonRequest = JsonConvert.SerializeObject(postData);
HttpContent content = new StringContent(jsonRequest, System.Text.Encoding.UTF8, "application/json");
var result = await httpClientRequest.PostAsync("http://myadress.com/test.php", content);
var resultString = await result.Content.ReadAsStringAsync();
return true;
}
你 php
代码如下:
$input = file_get_contents('php://input');
$value = json_decode($input, true);
if (!empty($value) && !empty($value['photo_data']) && !empty($value['photo_name'])) {
file_put_contents($value['photo_name'], base64_decode($value['photo_data']));
}
你看,当你调用 JsonConvert.SerializeObject(postData)
时,你的 byte[]
变成了一个 base64 编码的字符串。您正在 POST-body 中发送该数据目录。所以在 php
端你需要先 json_decode()
of php://input
然后 base64_decode()
of image bytes.