[thelist] PHP directory listing function

Aleem aleem.bawany at utoronto.ca
Mon Dec 30 22:09:00 CST 2002


Pasting some code from a function I wrote for just that (snipping
some parts so it may not work right off the bat):

<code>
        function listdir($targetdir = ".") {
                global $root, $path, $htmlRoot, $home;

                $i = 0;
                $dirlist = array();

                $targetdir = $root."/".$htmlRoot."/".$targetdir;

                $handle=opendir($targetdir);

                while (false !== ($file = readdir($handle))) {
                        if ($file != "." && $file != ".." && (substr($file,0,1) != "_")) {
                                // if (is_dir($targetdir.$file))
                                        $dirlist[$i] = $file;


                                $i++; // next
                        }
                }
          closedir($handle);
          return $dirlist;
        }
}
</code>

a couple of things to note, the file path must be absolute from the
root: /htdocs/site/dir/

the function orignally enumerated the directories within a directory
but I commented that line out to check for a dir:
if (is_dir($targetdir.$file)

it also ignore filenames beggining with _ since those are meant to be
hidden on my site since they contain server side code.

Refer to php docs if you need any further help.

Aleem





[ http://members.evolt.org/aleem/ ]




More information about the thelist mailing list