This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getContents($path) { | |
$contents = array_diff(scandir($path, SCANDIR_SORT_ASCENDING), array('..', '.')); // For Linux environments, excluded the .. and . from list | |
$filtered = array(); | |
foreach ($contents as $item) { | |
$itemPath = $path . '/' . $item; // scan_dir() requires an entire path, not just the filename (unless you cd in the directory) | |
if (is_dir($itemPath)){ // only keeps the directories | |
$subarray = array('name' => "$item", 'children' => getContents($itemPath)); // saves the folder name as `name` property and the children recursively | |
array_push($filtered, $subarray); |