Im creating a php page that reads a directory structure to be used as a menu system. i need to get all the directories and sub directories to be sorted alphabetically. i dont really have any idea where to start. im assuming i need to get all the data into a mulitidimensional array? help is much appreciated. below is the code im using.
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";
echo "<menu>\n";
function getDirectory($firstTry, $path = '.', $level = 1){
$ignore = array( 'cgi-bin','content','images','pages', '.', '..' );
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.
$dh = @opendir( $path );
// Open the directory to the handle $dh
while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory
if( !in_array( $file, $ignore ) ){
// Check that this file is not to be ignored
$spaces = str_repeat( ' ', ( $level * 4 ) );
// Just to add spacing to the list, to better
// show the directory tree.
if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...
if($level < 2){
if ($firstTry == 'false'){
echo "</category>\n";
}elseif($firstTry == 'true'){
$firstTry = 'false';
}
echo "<category Name=\"$file\">\n";
//echo "<strong>$spaces $file</strong><br />";
getDirectory('false',"$path/$file", ($level+1));
} else{
echo "<subcat Name=\"$file\"/>\n";
//echo "-$spaces $file<br />";
}
// Re-call this same function but on a new directory.
// this is what makes function recursive.
}
}
}
closedir( $dh );
// Close the directory handle
}
getDirectory('true',"." );
echo "</category>\n
</menu>\n";