note 24648 added to ref.dir

From: Date: Sat, 24 Aug 2002 11:46:09 +0000
Subject: note 24648 added to ref.dir
Groups: php.notes 
Request: Send a blank email to php-notes+get-35450@lists.php.net to get a copy of this message
i wrote this function that recursively explores a directory and its subdirectories, and returns the whole structure and all filenames as an XML tree. function tree2xml ($dir, $level = 0) { if ($handle = opendir ($dir)) { $tempFiles = array (); $tempSubDirs = array (); $space = str_repeat (' ', $level); $xml = $space . "<dir name='" . $dir . "'>\n"; while (false !== ($file = readdir ($handle))) { if ($file != '.' && $file != '..') { if (is_dir ($dir . '/' . $file)) { array_push ($tempSubDirs, $file); } else { array_push ($tempFiles, $file); } } } sort ($tempFiles); sort ($tempSubDirs); for ($i = 0; $i < count ($tempSubDirs); $i++) { $xml .= tree2xml ($dir . '/' . $tempSubDirs[$i], $level + 1); } for ($i = 0; $i < count ($tempFiles); $i++) { $xml .= $space . ' <file name=\'' . $tempFiles[$i] . "' />\n"; } $xml .= $space . "</dir>\n"; closedir ($handle); return $xml; } else return $dir; } // usage: echo tree2xml ('.'); -- http://www.php.net/manual/en/ref.dir.php http://master.php.net/manage/user-notes.php?action=edit+24648 http://master.php.net/manage/user-notes.php?action=delete+24648 http://master.php.net/manage/user-notes.php?action=reject+24648

« previous php.notes (#35450) next »