Skip to content

Instantly share code, notes, and snippets.

@GeHou
Last active August 29, 2015 13:56
Show Gist options
  • Save GeHou/8910876 to your computer and use it in GitHub Desktop.
Save GeHou/8910876 to your computer and use it in GitHub Desktop.
fileTree
<?php
/*
* Return path folders and files
* @param string $path Input path
* @param showFolder $path is show folder
* @param pattern $path glob function's pattern
* @return array
*/
function fileTree($path, $showFolder = false, $pattern = '*'){
$tree = array();
$nodeArr = glob(realpath($path) . '/' . $pattern);
foreach($nodeArr as $node){
if(is_dir($node)){
$rec = fileTree($node);
$tree['folder'][] = $node;
if($showFolder == true && isset($rec['folder'])) {
$tree['folder'] = array_merge($tree['folder'], $rec['folder']);
}
$tree['file'] = array_merge($tree['file'], $rec['file']);
} else{
$tree['file'][] = $node;
}
}
return $tree;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment