Page 1 of 1

Listing directory contents?

Posted: Sat Oct 25, 2003 1:17 am
by Albright
Anyone have a code sample or program that'll go through a directory and possibly load the contents of it in an array -- that works?

I found some samples out there on my own, but they all either have their own pesky errors or have the same problem my own attempts at writing directory-listing code does; no matter how many files are in the directory, it's "empty."

Posted: Sat Oct 25, 2003 1:46 am
by markl999

Code: Select all

if ($handle = opendir('/path/to/files')) {
    while (false !== ($file = readdir($handle))) {
        $array[] = $file;
    }
    closedir($handle);
}
var_dump($array); ///list the files
If you're on PHP 4.3.0 or greater also look at glob and if you're using PHP5 there's scandir

Posted: Sat Oct 25, 2003 4:55 am
by Albright
I knew it had to be that simple. Much thanks.

I'm sticking with PHP 4.3 for now, so no scandir() for me.

Thanks again... this one was annoying the heck out of me. You shall be honored in a comment block of my code. :)