vendredi 10 juin 2016

Upload image that I send from my frontend to my server PHP


Instead of putting the image directly into MySQL I am trying to store it on my server and then via a http/url-link reach the image.

But I need to take it one step at a time so what I firstly want to achieve is to get the image succesfully on my server/inside my http-addressfolder. (example: http://www.myadress.com/uploadedimages/)

On my frontend I send the imagename (the path of the file) and it is called "photo" and then I send the byte [] cointaining the imagedata and that one is called "photodata".

This is my current code.

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

$mysql_pekare= new mysqli ("serv", "user","pass", "db");

if(!empty($value)) {

print_r($value); //showing below what value I get here when I submit the form on my frontend

$binary=base64_decode($value->photodata); //adding my photodata here, should I decode it?
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploadedimages/'.$value->photo); //adding the photoname into my http://www.myadress.com/uploadedimages/
//trying to create the file
fwrite($file, $binary);
fclose($file);

}

?>

And when I send something from my frontend I reach the print_r and I get these values out. the Name of the photo and the photoData is null but as I send a file i assume it is supposed to be blank there?

stdClass Object
(
[photo] => /Users/Carl/Library/Developer/CoreSimulator/Devices/3231-01CE-4534-BBAD-7DBA4CB31175/data/Containers/Data/Application/32131C2320-6AD6-45B4-9775-8DF745E22321/Documents/temp/IMG_282610_194618.jpg

[photodata] => 

)

This is how I send it in on my frontend:

static public async Task <bool>  createPhotoTwo (string  image, byte [] imgData)
    {   
        var httpClientRequest = new HttpClient ();

        var postData = new Dictionary <string, object> ();

        postData.Add ("photo", image);
        postData.Add ("photodata", imgData);

        var jsonRequest = JsonConvert.SerializeObject(postData);

        HttpContent content = new StringContent(jsonRequest, System.Text.Encoding.UTF8, "application/json");

        var result = await httpClientRequest.PostAsync("http://www.myaddress.com/put.php", content);
        var resultString = await result.Content.ReadAsStringAsync ();
        return  true;
     }

Aucun commentaire:

Enregistrer un commentaire