Solved

List all subdirectories, subdirectory's directories as list

Posted on 2008-10-15
10
1,078 Views
Last Modified: 2008-10-15
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
0
Comment
Question by:0plus1
  • 6
  • 4
10 Comments
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22721753
what version of PHP?
0
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22721953
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

0
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22721979
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

0
 

Author Comment

by:0plus1
ID: 22722004
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
0
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22722293
try the second post, that problem was fixed in it :P
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:0plus1
ID: 22722504
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>

0
 
LVL 15

Accepted Solution

by:
MMDeveloper earned 100 total points
ID: 22723548
ah ok it was trying to print a listing of an empty directory. I have modified the code and this outputs valid html (per your example) for me.
<?php

print_r(makeULLI(readDirR()));
 

function readDirR($dir = "./") {

	$listing = opendir($dir);

	$return = array ();

	while(($entry = readdir($listing)) !== false) {

		if ($entry != "." && $entry != "..") {

			$item = $dir . "/" . $entry;

			if (is_dir($item)) {

				$return[$entry] = readDirR($item);

			} else {}

		} else {}

	}
 

	return $return;

}
 

function makeULLI($array) {

	$return = "<ol>\n";
 

	if (is_array($array) && count($array) > 0) {

		foreach ($array as $k => $v) {

			if (is_array($v) && count($v) > 0) {

				$return .= "\t<li>" . $k . makeULLI($v) . "</li>\n";

			}

			else {

				$return .= "\t<li>" . $k . "</li>\n";

			}

		}

	} else {}
 

	$return .= "</ol>";
 

	return $return;

}

?>

Open in new window

0
 

Author Comment

by:0plus1
ID: 22723863
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..
0
 
LVL 15

Assisted Solution

by:MMDeveloper
MMDeveloper earned 100 total points
ID: 22724286
yeah I'd try this modified function
function makeULLI($array, $printul = false) {

	if ($printul != false) {

        	$return = "<ol>\n";

	} else {}

 

        if (is_array($array) && count($array) > 0) {

                foreach ($array as $k => $v) {

                        if (is_array($v) && count($v) > 0) {

                                $return .= "\t<li>" . $k . makeULLI($v, true) . "</li>\n";

                        }

                        else {

                                $return .= "\t<li>" . $k . "</li>\n";

                        }

                }

        } else {}

 

	if ($printul != false) {

        	$return .= "</ol>";

	} else {}

 

        return $return;

}

Open in new window

0
 

Author Comment

by:0plus1
ID: 22724710
Thank you again, you spared me a lot of time.. i was stuck on this one!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mimic google as my ip 11 53
Paypal 502 Error 3 66
Download a website to hdd 2 49
Session timeout 5 13
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now