note 57804 deleted from ref.strings by googleguy

From: Date: Tue, 22 Oct 2013 14:54:16 +0000
Subject: note 57804 deleted from ref.strings by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-197217@lists.php.net to get a copy of this message
Note Submitter: ---- Updated; used to test for input as an array, should have been a string, added some validation of the input while I was at it. Enjoy. <?php /*---------- Directory Tree to Array ----------// Takes base directory, returns an associative array containing each directory name as an associative key and each file as an indexed entry in the appropriate directory. If no path is provided, it uses the current directory If $style == 1, it will return a sorted, non-associative array By Peter Dolan www.peterd.net Updated 2005-10-12 Use it as much as you like for whatever For my own entertainment, please let me know if you do //-----------------------------------------------------------*/ function dirTreeToArray( $path = "./", $style="0" ) { if ( !is_string($path) or !($style==0 or $style==1) ) die("Improper arguments in function dirTreeToArray(string \$path, \$style = {0, 1})"); else { $path = trim($path); $path = str_replace("\\", "/", $path); if( '/' == $path{strlen($path)-1} || '\'' == $path{strlen($path)-1} ) $path = substr($path, 0, strlen($path)-1); } $tree = array(); $arrayPointer = & $tree; if ( $handle = opendir($path) ) { while ( $fileName = readdir($handle) ) { if ( $fileName != "." and $fileName != ".." ) { $newPath = "$path/$fileName"; // Used for testing directory or file property if ( is_dir($newPath) ) { $dummyPointer = & $arrayPointer; // Save the current pointer if ( $style == 0 ) $arrayPointer = & $arrayPointer[$fileName]; // Drop the pointer into a subdirectory elseif ( $style == 1 ) $arrayPointer = & $arrayPointer[]; // Drop the pointer into a subdirectory $arrayPointer = dirTreeToArray($newPath, $style); // Process the new subdirectory if ( $style == 1 ) sort($arrayPointer); // Sort it if we're in a non-associative mode $arrayPointer = & $dummyPointer; // Bring the pointer back up a level } elseif ( is_file($newPath) ) $arrayPointer[] = $fileName; // Enter the files } } return $tree; } else die("Failed to open directory $path in function dirTreeToArray(string \$path, \$style = {0, 1})"); } ?>

« previous php.notes (#197217) next »