简体   繁体   中英

Send and image from PHP to Javascript (JSON)

I'm trying to make a "meal" in my DB, so in my website i made a form with a name, and a picture. This is the code of the form :

<?php
        $new_meal_title = htmlentities($_POST["new_meal_title"]);
        $new_meal_img = htmlentities($_POST["new_meal_img"]);

        $data = array(
            'new_meal_title' => $new_meal_title,
            'new_meal_img' => base64_encode($new_meal_img)
        );

        $options = array(
            'http' => array(
                'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                'method'  => 'POST',
                'content' => http_build_query($data)
            )
        );

        $context = stream_context_create($options);
        $result = file_get_contents(constant("API_URL")."/meal", false, $context);
        if($result === FALSE){
            var_dump($result);
        }

        $json = json_decode($result);

        if($json->success == "true"){
            header('Location: ../../');
            return;
        }
        else{
            echo $json->message;
        }
        header('Location: ../../');
?>

The form is sending data to my Node API. My Question is, how to save into a folder the image path through form in Javascript after it has been received in JSON.

Thanks for your help, i just had to change this line :

$new_meal_img = htmlentities($_POST["new_meal_img"]);

By

$new_meal_img = $_FILES['new_meal_img']["tmp_name"];

And

'new_meal_img' => base64_encode($new_meal_img)

By

'new_meal_img' => base64_encode(file_get_contents($new_meal_img))

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