从PHP发送和映像到Javascript(JSON)

时间:2016-06-27 09:17:43

标签: javascript php json image api

我正在尝试在我的数据库中制作一顿“饭”,所以在我的网站上我制作了一个带有名字和图片的表格。这是表格的代码:

<?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: ../../');
?>

表单正在向我的Node API发送数据。我的问题是,如何在JSON中收到Javascript后通过Javascript格式保存到文件夹中的图像路径。

1 个答案:

答案 0 :(得分:0)

感谢您的帮助,我只需更改此行:

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

通过

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

'new_meal_img' => base64_encode($new_meal_img)

通过

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