0

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;

}
medvedo
  • 543
  • 2
  • 8
  • 23
  • 1
    Show us what you get as the result of `print_r($value)` _Edit it into your question, dont post as a comment_ – RiggsFolly Jun 28 '16 at 20:31
  • Also how are you creating this data you call a `byte` array – RiggsFolly Jun 28 '16 at 20:33
  • I create it from my frontend (an app platform) that I send to this domain and code. – medvedo Jun 28 '16 at 20:34
  • 1
    1. What do you mean by "named" in "the byte array is named 'photo'"? It's an array of bytes, how can it be "named"? 2. In PHP, byte arrays are just strings, and can in any way be treated as such. 3. How do you "send" the file? Are you actually packing it in some JSON structure (which would likely break it, unless base64 encoded or so), or is that `json_decode` just the result of cargo cult programming? – Siguza Jun 28 '16 at 20:37
  • Updated the post: It is sent as a System.Text.Encoding.UTF8, "application/json". I was just following how you do it with a "standard" image upload from a PHP-form where you get the tmp_name and the name of the image. My goal is to upload it on my map on my domain so I assume i have to get the imagename out of it somehow first? – medvedo Jun 28 '16 at 20:40
  • indeed, we need to see your **an app platform** code, that is creating and sending data to `PHP` – spirit Jun 28 '16 at 20:41
  • The standard method you're talking about is used for multi-part form data uploads. Unless the data is base64 encoded, you cannot pack a `byte []` into a JSON document. If you are passing raw binary data into the PHP standard input, then you do not need to use `json_decode`. – SArnab Jun 28 '16 at 20:43
  • I just updated the post with how i send my image (it is c# however and not PHP). – medvedo Jun 28 '16 at 20:44
  • Yeah I will get it now riggs. it laggs alot when I try to copy it from the frontend. Give me a second – medvedo Jun 28 '16 at 20:44
  • When I try to copy it in here the computer just laggs out haha.. I assume it contains to much text for my own good haha. I will try to get it fixed. – medvedo Jun 28 '16 at 20:48
  • I have some of the text of it now and it is huge with just random letters. Updated the post with it. – medvedo Jun 28 '16 at 20:50
  • Computer froze when I tried to upload it again :( – medvedo Jun 28 '16 at 20:53
  • You can't get `image_name` and the `image_tmp_name` from your `C#` app simply because you didn't send it. Don't try to upload that result, give me a minute to test your code. – spirit Jun 28 '16 at 20:54
  • Okok. Is there a way to get the byte [] i send to the PHP-code uploaded as an image on the domainmap or do I need to send more things along with the byte to make this work? – medvedo Jun 28 '16 at 20:56
  • yeap. one moment please. =) – spirit Jun 28 '16 at 20:56
  • On my C# I can send like the imagepath as a string if that would help the process but maybe you have a solution that does not require that – medvedo Jun 28 '16 at 21:31
  • I've just managed to nake working your C# code, so soon it will be solution – spirit Jun 28 '16 at 21:47

1 Answers1

1

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.

spirit
  • 3,265
  • 2
  • 14
  • 29
  • imgName should that be the imagepath or just the name of the image? (just to clarify) – medvedo Jun 28 '16 at 22:09
  • It doesn't really matter, it's up to you. You can send imagePath from `C#`, or you can decide where to place image on the `php` side. – spirit Jun 28 '16 at 22:14
  • I think this code right here: `file_put_contents($value['photo_name'], base64_decode($value['photo_data']));` has to get replaced with this: `file_put_contents($value->photo_name, base64_decode($value->photo_data));` if I replace that code with this one I can reach a echo call inside there but maybe i am wrong? – medvedo Jun 28 '16 at 22:28
  • `$value = json_decode($input, true);` did you see second parameter? when it's **true**, returned value will be an array, not an object. I've tested my solution, it's working =)) – spirit Jun 28 '16 at 22:32
  • Ah I see! I did not see that. Thanks alot for this help. I will for sure upvote. Now I have to figure out how I get the image uploaded to the domainfolder, should be the easy part. I will probably make a new post within half an hour or something. Again, thanks alot for this help. – medvedo Jun 28 '16 at 22:36
  • uploaded it here: http://stackoverflow.com/questions/38087574/trying-to-upload-an-image-that-gets-sent-as-a-byte-to-my-domainfolder-server if u wanna take a look – medvedo Jun 28 '16 at 22:56