0

With this current code when I send a byte [] (and the imagename) from my frontend I receive this echo in the log "upload failed". I have an existing folder on my domain named "photosFolder" where I try to store my image and its imagename that I send from the frontend.

I am a bit confused now how to make this work so I added an move_uploaded_file that I frequently saw when I googled this issue. Is that the correct approach?

This is the PHP-code that I currently have.

<?php 

$input = file_get_contents('php://input');
$value = json_decode($input, true);
if (!empty($value)) {

file_put_contents($value['photo_name'], base64_decode($value['photo_data']));

$image_name = $value['photo_name'];
$image_data = $value['photo_data'];

if(move_uploaded_file($image_data, "photosFolder/$image_name")) {

 echo "image uploaded!";   
}

else {
    echo "upload failed";
 }
}

?>

I send my byte [] like this from my frontend:

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;
}
medvedo
  • 543
  • 2
  • 8
  • 23
  • Probably file size error, please read this : http://stackoverflow.com/questions/9921224/large-file-uploads-failing-php –  Jun 28 '16 at 23:01
  • I work with a domain and i cannot find the "htaccess file" in my "file manager". do I need to talk to the support about this you think and make them change this or should the htaccess file be there inside the domainfiles somewhere? – medvedo Jun 28 '16 at 23:17
  • do you have write permissions on the folder? – Terry Kernan Jun 28 '16 at 23:21
  • If you mean if I can make and also delete content, like new .php files and folders with images, then yes – medvedo Jun 28 '16 at 23:24
  • var_dump(ini_get('upload_max_filesize')); gives me string(3) "96M" so it is not about imagesize – medvedo Jun 29 '16 at 12:41

1 Answers1

1

Suppose I'm sending a transparent gif 1x1 size. In the real life size of this gif will be 47 bytes. For this reason I'll call your C# method like this:

await createPhotoThree("transparent.gif", imgData);

where imgData is a byte[47], containing that 47 bytes of transparent gif.

In this case, at the php side I will recieve from php://input the following:

{"photo_data":"R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOwAAAAA=","photo_name":"transparent.gif"}

So, all I need to do is just $value = json_decode($input, true); to make an array from that input. And, put somewhere recieved file. At your case, you need to change your php code like this:

$input = file_get_contents('php://input');
$value = json_decode($input, true);
if (!empty($value)) {
    $image_name = $value['photo_name'];
    $image_data = base64_decode($value['photo_data']);
    if(file_put_contents("photosFolder/$image_name", $image_data)) {
        echo "image uploaded!";
    } else {
        echo "upload failed";
    }
} else {
    echo "json error";
}

move_uploaded_file() will work only with regular file uploads, but to make a regular file upload, just like the browser it does, you'll need to setup HTTP header

Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryz0vLt59InlueCCBI

and properly form a POST-body, somthing like this:

------WebKitFormBoundaryz0vLt59InlueCCBI
Content-Disposition: form-data; name="uploaded_file"; filename="transparent.gif"
Content-Type: image/gif

<image content goes here>
------WebKitFormBoundaryz0vLt59InlueCCBI--

and work with it from $_FILES array and in that case move_uploaded_file() will work as desired.

I think, working with json a little bit easier than with regular file upload. Of curse, here we some sequrity issues, like checking from who we are recieving uploaded file, but that's another topic =)

spirit
  • 3,265
  • 2
  • 14
  • 29
  • With that PHP-code. What about base64_decode? I suppose I need to implement that as well? – medvedo Jun 29 '16 at 10:22
  • Ops... Forget that one. Fixed my answer – spirit Jun 29 '16 at 10:36
  • If I for example do not have the imagename, could I make one up myself or will that not work. So change this one: `"photosFolder/$image_name")` to this instead: `photosFolder/mynewcreatedphotoname.jpg`. So I create my own name instead of the original name of the image? Right now the $image_name variable is not the imagename, it is the imagepath so that is why I am wondering because right now with that code i get "upload failed". – medvedo Jun 29 '16 at 10:41
  • Yes, you can use imagename that you make at `php`. It will work. If your `$image_name` contains path, you'll need to do additional work. Like a: `$dirname = dirname("photosFolder/$image_name"); mkdir($dirname, 0777, true);` – spirit Jun 29 '16 at 10:50
  • Yeah I got the image_name out now with a simple basename (); But I still get upload failed unfortantly with your current code. Is there anyway I can see why it does not get added inside this one: `if(file_put_contents($image_data, "photosFolder/$image_name")) { echo "image uploaded!"; }` – medvedo Jun 29 '16 at 10:54
  • try this: `var_dump($image_name); var_dump(strlen($image_data)); $result = file_put_contents($image_data, "photosFolder/$image_name"); var_dump($result);` and tell what did you get in logs. – spirit Jun 29 '16 at 11:04
  • `var_dump($image_name)` gives me this: `string(23) "IMG_20160629_123904.jpg"` and the: `$result = file_put_contents($image_data, "photosFolder/$image_name"); var_dump($result);` gives me this: `NULL` – medvedo Jun 29 '16 at 11:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115962/discussion-between-spirit-and-medvedo). – spirit Jun 29 '16 at 12:05