note 54922 added to ref.dir

From: Date: Tue, 19 Jul 2005 18:26:33 +0000
Subject: note 54922 added to ref.dir
Groups: php.notes 
Request: Send a blank email to php-notes+get-92181@lists.php.net to get a copy of this message
A quick recursive function I came up with to return the directory tree and files under a particular path in an associative array. For a file such as /the/directory/you/want/thefile.foo, $tree["the"]["directory"]["you"]["want"][0] = "thefile.foo" I needed it for a hacktastic way of storing variables temporarily in a directory structure. YMMV. <?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 //-----------------------------------------------------------*/ function dirTreeToArray( $path = "./", $style="0" ) { if ( !is_array($path) or !($style==0 or $style==1) ) die("Improper arguments in function dirTreeToArray(string \$path, \$style = {0, 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})"); } ?> ---- Manual Page -- http://www.php.net/manual/en/ref.dir.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54922 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54922&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54922&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54922&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54922&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54922&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54922&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54922&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#92181) next »