Listing directory contents?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Albright
Forum Newbie
Posts: 20
Joined: Sat Sep 13, 2003 8:03 pm
Contact:

Listing directory contents?

Post 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."
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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
User avatar
Albright
Forum Newbie
Posts: 20
Joined: Sat Sep 13, 2003 8:03 pm
Contact:

Post 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. :)
Post Reply