1

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.

5
  • It looks like your JSON is not valid. Try validating it jsonlint.com first and debugging it once it is valid. Commented Oct 21, 2016 at 14:12
  • Instead of echo $json_obj to learn it's contents, do the following: echo ( "<pre>" ); print_r($json_obj); echo ( "</pre>" ); using that you will be able to easily see what items you are attempting to get. - for example: $json_obj->myfile (or it might be $json_obj['myfile'], i forget right now) Commented Oct 21, 2016 at 14:13
  • try json_decode($json,true); Commented Oct 21, 2016 at 14:57
  • I have corrected my json and now tell me the php code for getting the values. Commented Oct 22, 2016 at 6:07
  • You have to check out PHP manual again about json_decode. You don't have to use third party tool to check the validity of the JSON encoded string, because, json_decode() will return null if it is not encoded properly. The second parameter of json_decode(), that you have not used it, will determine if the returned value of json_decode is associative array or an object Commented Oct 22, 2016 at 17:01

1 Answer 1

3

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'];
Sign up to request clarification or add additional context in comments.

5 Comments

It shows error -- Notice: Trying to get property of non-object in D:\xampp\htdocs\work\filesharing\2.php on line 12
As I said, if that is your json, it's not a valid one. You need to format it properly before using with PHP.
I have corrected my json and now tell me the php code for getting the values.
Your json can be better, but anyway... I edited the answer
Yes, that i have tested

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.