Sentinel is working on many amazing projects, including ones implementing custom-tailored online file storage.

Files and folders are not stored on the server, and structure is maintained in database, meaning files can be spread across multiple file sharing/hosting services.

File storage service wouldn’t be fun if you don’t have folders. Or sub folders. And subsub….sub folders.

Nice, now we have infinitely deep folder structure, which makes searching difficult, and writing single or multiple database queries is out of the question, and let’s say you already have a method which returns entire file system tree (with files and folders).

Our file system tree may look like this:

{
  "name": "top folder",
  "type": "folder",
  "items": [
    {
      "type": "folder",
      "name": "sub 1",
      "path": "top folder/sub 1",
      "items": [
        {
          "type": "folder",
          "name": "sub 1 1",
          "path": "top folder/sub 1/sub 1 1",
          "items": [
            {
              "name": "someFile.txt",
              "type": "file",
              "path": "top folder/sub 1/sub 1 1/someFile.txt",
              "real_file_location": "http://examplestorage.com/files/file.txt"
            }
          ]
        },
        {
          "name": "someScript.php",
          "type": "file",
          "path": "/top folder/sub 1/someScript.php",
          "real_file_location":"http://storage2example.net/filez/file.php"
        }
      ]
    },
    {
      "type": "folder",
      "name": "sub 2",
      "path": "top folder/sub 2",
      "items": [

      ]
    },
    {
      "name": "fileInRoot.zip",
      "type": "file",
      "path": "top folder/fileInRoot.zip",
      "real_file_location": "http://example3storage.co.uk/storage/file.zip"
    }
  ]
}

 

We need to put all elements into one single dimensional array, so we can easily build search results array:

    $single_dim_array = multidim_to_singledim_array(json_decode($json_from_above,true));
    function multidim_to_singledim_array($multidim_arr, $path='', $level=0) {

        $arr=array();

        $temp=array();
        $temp['value']=$multidim_arr['name'];
        $temp['label'] = (($multidim_arr['type']==='file')?'file':'folder');
        $temp['url'] = site_url('#'.rawurlencode( (($level==0)?'':$path . '/') . $temp['value']));
        $temp['id'] = $multidim_arr['id'];
        $arr []= $temp;

        foreach($multidim_arr['items'] as $item){

                $stemp=array();
                $stemp['value']=$item['name'];
                $stemp['label'] = (($item['type']==='file')?'file':'folder');
                $stemp['url'] = site_url('#'.rawurlencode( (($level==0)?$temp['value'] .'/':$path . '/' . $temp['value'] .'/') . $stemp['value']));
                $stemp['id'] = $item['id'];
                $arr []= $stemp;

                $items = array_map(function ($v){if (is_array($v) || is_object($v)) {return $v;}return $v;}, $item['items']);
                $items = array_filter($items,function($v){if($v[0]){return false;}else{return $v;}});

                foreach($items as $sitem){
                    if($sitem['type']==='file'){
                        $stemp=array();
                        $stemp['value']=$sitem['name'];
                        $stemp['label'] = (($sitem['type']==='file')?'file':'folder');
                        $stemp['url'] = site_url('#'.rawurlencode((($level==0)?'':$path .'/').$temp['value'].'/'. $item['name'] . '/' . $stemp['value']));
                        $stemp['id'] = $sitem['id'];
                        $arr []= $stemp;
                    }else{
                        $more_items = multidim_to_singledim_array($sitem,$path.'/'.$item['name'],($level+1));

                        foreach($more_items as $mitem){
                            $arr []= $mitem;
                        }
                    }
                }
        }

       return $arr;
    }

 

Results are:

[
    {
       "type": "folder",
       "name": "sub 1",
       "path": "top folder"
    },
    {
       "type": "folder",
       "name": "sub 1",
       "path": "top folder/sub 1"
    },
   {
      "type": "folder",
      "name": "sub 1 1",
      "path": "top folder/sub 1/sub 1 1"
    },
    {
      "name": "someFile.txt",
      "type": "file",
      "path": "top folder/sub 1/sub 1 1/someFile.txt"
    },
    ...
]

 

Now you can iterate through $single_dim_array and pick only items which contain search string in item name for example, and then merge these results with results from another entity (users, posts, pages) etc.

Need custom solution? Contact Us