Link to home
Start Free TrialLog in
Avatar of 0plus1
0plus1Flag for Italy

asked on

List all subdirectories, subdirectory's directories as list

I have a directory structure like this one

main/category/name

each name directory has a bunch of subdirectories and files that we don't need to consider

I need a php script that can read the contents of the main folder and print out a list in a similar fashion:

               <li>category
                  <ul>
                  <li>name1</li>
                  <li>name2></li>
                  <li>namen></li>
                  </ul>
                </li>
               <li>category2
                  <ul>
                  <li>name1</li>
                  <li>name2></li>
                  <li>namen></li>
                  </ul>
                </li>

Until no more categories are found. How can i achieve this?

Thank you very much
Avatar of MMDeveloper
MMDeveloper
Flag of United States of America image

what version of PHP?
ok well either way, try this script, just feed it the directory path..
<?php
print_r(makeULLI(readDirR("./skins")));
 
function readDirR($dir = "./") {
	$listing = opendir($dir);
	$return = array ();
	while(($entry = readdir($listing)) !== false) {
		if ($entry != "." && $entry != "..") {
			$item = $dir . "/" . $entry;
			//echo $item . "<br />";
			if (is_file($item)) {
				$return[] = $entry;
			}
			elseif (is_dir($item)) {
				$return[$entry] = readDirR($item);
			}
		} else {}
	}
 
	return $return;
}
 
function makeULLI($array) {
	$return = "<ul>";
 
	if (is_array($array) && count($array) > 0) {
		foreach ($array as $k => $v) {
			if (is_array($v)) {
				$return .= "<li>" . $v . makeULLI($v) . "</li>";
			}
			else {
				$return .= "<li>" . $v . "</li>";
			}
		}
	} else {}
 
	$return .= "</ul>";
 
	return $return;
}
?>

Open in new window

sorry you didn't want the files to be there. here is a modified version that only looks at directories.
<?php
print_r(makeULLI(readDirR("./skins")));
 
function readDirR($dir = "./") {
	$listing = opendir($dir);
	$return = array ();
	while(($entry = readdir($listing)) !== false) {
		if ($entry != "." && $entry != "..") {
			$item = $dir . "/" . $entry;
			if (is_file($item)) {
				//$return[] = $entry;
			}
			elseif (is_dir($item)) {
				$return[$entry] = readDirR($item);
			}
		} else {}
	}
 
	return $return;
}
 
function makeULLI($array) {
	$return = "<ol>";
 
	if (is_array($array) && count($array) > 0) {
		foreach ($array as $k => $v) {
			if (is_array($v)) {
				$return .= "<li>" . $k . makeULLI($v) . "</li>";
			}
			else {
				$return .= "<li>" . $v . "</li>";
			}
		}
	} else {}
 
	$return .= "</ol>";
 
	return $return;
}
?>

Open in new window

Avatar of 0plus1

ASKER

php version: 5

Your script fails because prints this:

# Array

# Array

# Array

    * Array
          o nomefile
          o nomefile
          o nomefile

# Array

# Array

A parte fromt the array error, it shouldn't print the file inside the subfolder, instead it should ignore them completely
try the second post, that problem was fixed in it :P
Avatar of 0plus1

ASKER

We are getting there, the problem now is this i want exactly this fashion:

              <li>category
                  <ul>
                  <li>name1</li>
                  <li>name2></li>
                  <li>namen></li>
                  </ul>
                </li>

while your script outputs this:

<ul>
<li>category
<ul>
<li>name1<ul></ul></li>
<li>name2<ul></ul></li>
</ul>
</li>

ASKER CERTIFIED SOLUTION
Avatar of MMDeveloper
MMDeveloper
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 0plus1

ASKER

Thank you man
almost there (sorry to be so finicky) the last thing is that i don't want the first <ul> tag, i know it seems strange and it's not valid html but this is going to be part of a suckefish style menu and i need a custum ul tag at the beginning. So to get ONLY the first <ul> tag what should i do? Put an if condition?

Thanks again man, you are my saviour..
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 0plus1

ASKER

Thank you again, you spared me a lot of time.. i was stuck on this one!