Crap, this is in one of my books on how to do it.
Here's a recursive one:
http://www.phpbuilder.com/snippet/do...snippet&id=747
Here's another:
http://www.phpbuilder.com/snippet/do...snippet&id=726
I'm sure with a little tweaking, you could get either to work.