Return to Snippet

Revision: 40843
at February 8, 2011 21:50 by danoph


Updated Code
/* to use:
pattern = glob pattern to match
flags = glob flags
path = path to search
depth = how deep to travel, -1 for unlimited, 0 for only current directory
*/

$folders = folder_tree('*.*', 0, '/path_here/', -1);

print_r($folders);

function folder_tree($pattern = '*', $flags = 0, $path = false, $depth = 0, $level = 0) {
	$tree = array();
	
	$files = glob($path.$pattern, $flags);
	$paths = glob($path.'*', GLOB_ONLYDIR|GLOB_NOSORT);
	
	if (!empty($paths) && ($level < $depth || $depth == -1)) {
		$level++;
		foreach ($paths as $sub_path) {
			$tree[$sub_path] = folder_tree($pattern, $flags, $sub_path.DIRECTORY_SEPARATOR, $depth, $level);
		}	
	}

	$tree = array_merge($tree, $files);
	
	return $tree;
}

Revision: 40842
at February 8, 2011 21:47 by danoph


Updated Code
/* to use:
pattern = glob pattern to match
flags = glob flags
path = path to search
depth = how deep to travel, -1 for unlimited, 0 for only current directory
*/

$folders = folder_tree('*.*', 0, '/path_here/', -1);

print_r($folders);

function folder_tree($pattern = '*', $flags = 0, $path = false, $depth = 0, $level = 0) {
	$tree = array();
	
	$files = glob($path.$pattern, $flags);
	$paths = glob($path.'*', GLOB_ONLYDIR|GLOB_NOSORT);
	
	if (!empty($paths) && ($level <= $depth || $depth == -1)) {
		$level++;
		foreach ($paths as $sub_path) {
			$tree[$sub_path] = folder_tree($pattern, $flags, $sub_path.DIRECTORY_SEPARATOR, $depth, $level);
		}	
	}

	$tree = array_merge($tree, $files);
	
	return $tree;
}

Revision: 40841
at February 8, 2011 21:43 by danoph


Updated Code
/* to use,
$folders = folder_tree('*.*', 0, '/path_here/', -1);

print_r($folders);
*/

function folder_tree($pattern = '*', $flags = 0, $path = false, $depth = 0, $level = 0) {
	$tree = array();
	
	$files = glob($path.$pattern, $flags);
	$paths = glob($path.'*', GLOB_ONLYDIR|GLOB_NOSORT);
	
	if (!empty($paths) && ($level <= $depth || $depth == -1)) {
		$level++;
		foreach ($paths as $sub_path) {
			$tree[$sub_path] = folder_tree($pattern, $flags, $sub_path.DIRECTORY_SEPARATOR, $depth, $level);
		}	
	}

	$tree = array_merge($tree, $files);
	
	return $tree;
}

Revision: 40840
at February 8, 2011 21:42 by danoph


Updated Code
function folder_tree($pattern = '*', $flags = 0, $path = false, $depth = 0, $level = 0) {
	$tree = array();
	
	$files = glob($path.$pattern, $flags);
	$paths = glob($path.'*', GLOB_ONLYDIR|GLOB_NOSORT);
	
	if (!empty($paths) && ($level <= $depth || $depth == -1)) {
		$level++;
		foreach ($paths as $sub_path) {
			$tree[$sub_path] = folder_tree($pattern, $flags, $sub_path.DIRECTORY_SEPARATOR, $depth, $level);
		}	
	}

	$tree = array_merge($tree, $files);
	
	return $tree;
}

Revision: 40839
at February 8, 2011 21:41 by danoph


Initial Code
function folder_tree($pattern = '*', $flags = 0, $path = false, $depth = 0, $level = 0) {
	$tree = array();
	
	$files = glob($path.$pattern, $flags);
	$paths = glob($path.'*', GLOB_ONLYDIR|GLOB_NOSORT);
	
	if (!empty($paths) && ($level <= $depth || $depth == -1)) {
		$level++;
		foreach ($paths as $sub_path) {
			$tree[$sub_path] = folder_tree('*', $flags, $sub_path.DIRECTORY_SEPARATOR, $depth, $level);
		}	
	}

	$tree = array_merge($tree, $files);
	
	return $tree;
}

Initial URL

                                

Initial Description
There are a lot of recursive glob functions out there but they don't build a tree, they build a 1 dimensional array...This function builds a directory tree.

Initial Title
Recursive Glob File Tree

Initial Tags
file, directory

Initial Language
PHP