note 54344 added to function.scandir

From: Date: Thu, 30 Jun 2005 23:54:48 +0000
Subject: note 54344 added to function.scandir
Groups: php.notes 
Request: Send a blank email to php-notes+get-91226@lists.php.net to get a copy of this message
I made this to represent a directory tree in an array that uses the file or directory names as keys and full paths as the value for files. Directories are nested arrays. <?php function generatePathTree($rootPath) { $pathStack = array($rootPath); $contentsRoot = array(); $contents = &$contentsRoot; while ($path = array_pop($pathStack)) { $contents[basename($path)] = array(); $contents = &$contents[basename($path)]; foreach (scandir($path) as $filename) { if ('.' != substr($filename, 0, 1)) { $newPath = $path.'/'.$filename; if (is_dir($newPath)) { array_push($pathStack, $newPath); $contents[basename($newPath)] = array(); } else { $contents[basename($filename)] = $newPath; } } } } return $contentsRoot[basename($rootPath)]; } ?> The function will return something like this: Array ( [index.php] => /usr/local/www/index.php [js] => Array ( [async.js] => /usr/local/www/js/async.js [dom.js] => /usr/local/www/js/dom.js [effects.js] => /usr/local/www/js/effects.js [prototype.js] => /usr/local/www/js/prototype.js ) [logo.png] => /usr/local/www/logo.png [server.php] => /usr/local/www/server.php [test.js] => /usr/local/www/test.js ) ---- Manual Page -- http://www.php.net/manual/en/function.scandir.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54344 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54344&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54344&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54344&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54344&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54344&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54344&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54344&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#91226) next »