Reputation: 299
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.
Upvotes: 1
Views: 3214
Reputation: 689
If you just want to access the description property of your json, you can do the following:
$json_obj = json_decode($json, true);
$description = $json_obj[''][0]['description'];
Upvotes: 3