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