Welcome Guest, Not a member yet? Register   Sign In
Directory tree as an associative array?

Consider the following:


What I'd like to do is access this structure as an associative array:

echo $array['dir1']['file1'];
echo $array['dir2']['file2'];

I can figure out how to write the file contents from the files, the real issue I'm having is finding a native or non native php function for listing directories as an associative array.

I'm not sure if it's a wise idea. If you were to call this function in a directory that contained many levels of directories, you could end up using a very large amount of memory, or possible even exhausting it completely. Calling a function recursively like that on a potentially unlimited result set could be a real painus in the anus.

Can I ask what you're hoping to achieve, as there's probably a much better solution?

Here's a function that will allow limited recursion.
# Original PHP code by Chirp Internet: www.chirp.com.au
# Please acknowledge use of this code by including this header.

function getFileList($dir, $recurse=false, $depth=false)
    # array to hold return value
    $retval = array();

    # add trailing slash if missing
    if(substr($dir, -1) != "/") $dir .= "/";

    # open pointer to directory and read list of files
    $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
    while(false !== ($entry = $d->read())) {
        # skip hidden files
        if($entry[0] == ".") continue;
        if(is_dir("$dir$entry")) {
            $retval[] = array(
                    "name" => "$dir$entry/",
                    "type" => filetype("$dir$entry"),
                    "size" => 0,
                    "lastmod" => filemtime("$dir$entry")
            if($recurse && is_readable("$dir$entry/")) {
                if($depth === false) {
                    $retval = array_merge($retval, getFileList("$dir$entry/", true));
                } elseif($depth > 0) {
                    $retval = array_merge($retval, getFileList("$dir$entry/", true, $depth-1));
        } elseif(is_readable("$dir$entry")) {
            $retval[] = array(
                    "name" => "$dir$entry",
                    "type" => mime_content_type("$dir$entry"),
                    "size" => filesize("$dir$entry"),
                    "lastmod" => filemtime("$dir$entry")

    return $retval;

Basically, its going to scan the directory that contains my style sheets and directories that contain style sheets. They are then compiled in to a single css file however different style sheets are handled differently based what directories they are in.

I'm sure you have your reasons, but it sounds very complex to me. I use a little asset management library I created which allows me to configure default styles that will show on every page, and add more styles, script or meta data from within the controller or view with a function call. Would you be interested in that? There are some very good asset management libraries out there, but all the ones I found seemed like overkill for what I needed.

Theme © iAndrew 2016 - Forum software by © MyBB