简体   繁体   中英

How do I get the image_name and image_tmp_name from a byte [] in PHP?

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;

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM