/** * Map * * Recusrively gets all files and folders in the directory, with an optional depth limit * * @param string the path to the folder * @param number how many levels to process * @return array The list of files and folders */ public static function map($path, $levels = NULL, $structured = FALSE, $files_first = FALSE) { // trim trailing slashes $levels = is_null($levels) ? -1 : $levels; $path = preg_replace('|/+$|', '', $path); // filesystem objects $files = array(); $folders = array(); $objects = array_diff(scandir($path), array('.', '..')); // check through foreach ($objects as $v) { $dir = $path . '/' . $v; if (is_dir($dir)) { $folders[$v] = $levels != 0 ? filesystem::map($dir, $levels - 1, $structured, $files_first) : $v; } else { array_push($files, $v); } } // return if ($structured) { return array('/folders' => $folders, '/files' => $files); } else { return $files_first ? array_merge($files, $folders) : array_merge($folders, $files); } }