douhang5493 2016-06-28 20:26
浏览 228
已采纳

如何从PHP中的byte []获取image_name和image_tmp_name?

I am currently sending an byte [] from my frontend into my domain and PHP-code. The byte array that I am sending is named "photo" and it is sent like a System.Text.Encoding.UTF8, "application/json". My goal is to get the byte [] into an image and then upload it my domainfolder (that already exists) do I need to get the image_name and the image_tmp_name from that byte [] in order to do this? I am a bit uncertain on how exactly I should get this to work.

I currently have this code:

<?php 

$value = json_decode(file_get_contents('php://input'));

if(!empty($value)) {

print_r($value);
}

?>

With this code the print gives me a huge line of text containing the byte [].

How do I now get the image_name and image_tmp_name from this byte []? My goal is to upload the image on my domainmap (named photoFolder that already exists) with a code looking something like this:

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

}

How i send it:

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;

}
  • 写回答

1条回答 默认 最新

  • dook0034 2016-06-28 22:05
    关注

    Here is solution. Change your C# method like this:

    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;
    }
    

    and you php code like this:

    $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']));
    }
    

    You see, when you calling JsonConvert.SerializeObject(postData) your byte[] becames a base64-encoded string. And you are sending that data directry in the POST-body. So at the php side you need to json_decode() of php://input first and then base64_decode() of image bytes.

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

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题