I want to convert json data to String in php. This is my json data
{
"": [{
"description": "hello, this is description speaking"
}, {
"fieldName": "myfile",
"originalFilename": "image.png",
"path": "/var/folders/rq/q_m4_21j3lqf1lw48fqttx_80000gn/T/ttzVoNPfBxMMirec1tJsnrd2.png",
"headers": "[Object]",
"size": "82745"
}]
}
I want to print "hello, this is description speaking" in my php file and also i want to upload my file. My php code is given below please correct it.
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$json = file_get_contents("php://input");
$json_obj = json_decode($json);
echo $json_obj;
echo $_POST['description'];
$name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];
$error = $_FILES['myfile']['error'];
if (!empty($name)) {
$location = 'uploads/';
if (move_uploaded_file($tmp_name, $location.$name)){
echo 'Uploaded';
}
} else {
echo 'please choose a file';
}
} else {
echo "error 2";
}
In this code errors are Undefined index: file in D:\xampp\htdocs\work. And without the fileupload code it shows nothing.
json_decode($json,true);json_decode. You don't have to use third party tool to check the validity of the JSON encoded string, because,json_decode()will returnnullif it is not encoded properly. The second parameter ofjson_decode(), that you have not used it, will determine if the returned value ofjson_decodeis associative array or an object