1

I am trying to build a web based file manager where I can view, create, edit, and delete folders and files. I need a data structure for storing the information about the files folders and subfolders.

I was trying JSON, but the problem is that there can be any number of subfolders inside subfolders which will come dynamically.

I wrote by hand a JSON test file, which the link is given below

{
  "home": {
    "file1": {
      "type": "file",
      "name": "test",
      "ext": "pdf",
      "date": "12/03/2015"
    },
    "file2": {
      "type": "file",
      "name": "test",
      "ext": "doc",
      "date": "31/01/2010"
    },
    "folder1": {
      "type": "folder",
      "name": "folder1",
      "date": "11/01/2010",
      "in": {
        "file": {
          "type": "file",
          "name": "test3",
          "ext": "pdf",
          "date": "23/01/2017"
        },
        "folder2": {
          "type": "folder",
          "name": "folder2",
          "date": "22/03/2011",
          "in": {
            "file4": {
              "type": "file",
              "name": "test3",
              "ext": "pdf",
              "date": "23/01/2017"
            }
          }
        },
        "folder4": {
          "type": "folder",
          "name": "folder4",
          "date": "11/09/2009",
          "in": {
            "file5": {
              "type": "file:",
              "name": "file5",
              "date": "11/09/2011"
            }
          }
        }
      }
    },
    "folder7": {
      "type": "folder",
      "name": "folder7",
      "date": "23/08/2015",
      "in": {
        "file7": {
          "type": "file",
          "name": "file7",
          "date": "11/01/2016"
        }
      }
    }
  }
}

There will be a dynamic generation of folders and subfolders which needed to be updated in the JSON file.

How can I access all the data from the JSON file after encoding into a variable in PHP or JavaScript, and update the file with new data as well?

Thank you

Gayan Hewa
  • 2,277
  • 4
  • 22
  • 41
Ep Sooraj
  • 21
  • 1
  • 7

3 Answers3

0

Read the file using file_open()

Parse the json using json_decode

Do whatever you want with your arrays

encode the json again with json_encode

write it to your file again with fwrite()

Your code should look like this:

<?php
 $json = fread($myfile,filesize("json.txt"));
 $json = json_decode($json);
 //json is now an array, do whatever you want with it
 $json = json_encode($json);
 fwrite(file,json);
Lucas Meine
  • 1,524
  • 4
  • 23
  • 32
0

The below snippet should work, it basically does a json_decode($str, true) of the JSON string :

$str = '{
    "type": "folder",
    "name": "folder1",
    "date": "11/01/2010",
    "in": {
        "file": {
            "type": "file",
            "name": "test3",
            "ext": "pdf",
            "date": "23/01/2017"
        },
        "folder2": {
            "type": "folder",
            "name": "folder2",
            "date": "22/03/2011",
            "in": {
                "file4": {
                    "type": "file",
                    "name": "test3",
                    "ext": "pdf",
                    "date": "23/01/2017"
                }
            }
        },
        "folder4": {
            "type": "folder",
            "name": "folder4",
            "date": "11/09/2009",
            "in": {
                "file5": {
                    "type": "file:",
                    "name": "file5",
                    "date": "11/09/2011"
                }
            }
        }
    }
}';


var_dump(json_decode($str, true));
Gayan Hewa
  • 2,277
  • 4
  • 22
  • 41
0

Your code should look like this:

<?php
 $json = file_get_contents('json.txt'); // read the file contains JSON code
 $array = json_decode($json); // convert json to normal php array

 //json is now a normal php array, do whatever you want with it

 echo '<pre>';
 print_r($array);
 echo '</pre>';

 $json = json_encode($array);
 file_put_contents('json.txt');