Jump to content

List Directory


N-Bomb(Nerd)

Recommended Posts

Hello,

 

I'm trying to walk a directory into a single array from a specific starting point. For example, lets say that I wanted to start at the specific directory "/content/users/n-bomb/" it would then begin to parse from that directory and list all sub-directories and directories and store them into an array. I'm not sure what would be the best array structure, but since I know the directory where I'm starting from (members base directory) I just want the array to be from their home directory. Ideally, I'd want a full-path to each directory as well, but I'm not sure how to incorporate that into the array structure.

 

Example array of "/content/users/n-bomb/":

[n-bomb] => Array
(
[0] => /content/users/n-bomb/form.vb
[1] => /content/users/n-bomb/song.mp3
[2] => /content/users/n-bomb/movie.avi
[images] => Array
(
	[0] => /content/users/n-bomb/images/funny.png
	[1] => /content/users/n-bomb/images/mad.png
	[2] => /content/users/n-bomb/images/sad.png
)
[documents] => Array
(
	[porn links] => Array
	(
		[0] => /content/users/n-bomb/porn links/good websites.txt
		[1] => /content/users/n-bomb/porn links/bad websites.txt
	)
	[0] => /content/users/n-bomb/documents/bills.txt
	[1] => /content/users/n-bomb/documents/website.txt
	[2] => /content/users/n-bomb/documents/emails.txt
)
)

 

I started to mock up some code, but I can't quite get it right:

    private function walk($directory, $start = False)
    {
        if(!is_dir($directory))
            return false;

        if(!$start)
            $this->information["structure"][basename($directory)] = array();

        $scan = scandir($directory);
        natcasesort($scan);

        foreach($scan as $item)
        {

            if($item == "." || $item == "..")
                continue;

            $item = $directory . "/" . $item;

            if(is_dir($item))
                $this->walk($item);

            if(is_file($item))
                ($start) ? $this->information["structure"][] = $item : $this->information["structure"][basename($directory)][] = $item;

        }
    }

 

With the above code I'm getting results like this:

Array
(
    [documents] => Array
(
	[0] => /content/users/n-bomb/documents/bills.txt
	[1] => /content/users/n-bomb/documents/website.txt
	[2] => /content/users/n-bomb/documents/emails.txt
)

    [porn links] => Array
(
	[0] => /content/users/n-bomb/porn links/good websites.txt
	[1] => /content/users/n-bomb/porn links/bad websites.txt
)
)

 

As you can see that "porn links" is not going inside of "documents". How can I resolve this issue so my code will output like the array structure that I want?

Link to comment
https://forums.phpfreaks.com/topic/255142-list-directory/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.