Directory listing

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
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Directory listing

Post by SBro »

I have a function I'm trying to build whereby it searches directories for files of a specific type and places those files into an array. Here is the code I have.

Code: Select all

function get_file_extension($filename) {
    $extension = explode (".", $filename);

    // -1 due to array starting with 0
    $exten = (count($extension) - 1);

    // Return it..
    return $extension[$exten];
}

function read_dir_into_array($folder, $ext = 'jpg') {
    global $files;
    if ($handle = opendir($folder)) {
    	while (false !== ($file = readdir($handle))) {
	    // If the file is not .current dir, or ..up one dir, then continue
            if ($file != "." && $file != "..") {
                $file_ext = get_file_extension($file);
                if ($file_ext == $file) {
                    read_dir_into_array($folder.$file);
                }
                elseif ($file_ext == $ext) {
                    $file = $folder . "/" . $file;
	            // replace // with /
    	            $files[] = preg_replace("/\/\//si", "/", $file);
                }
            }
        }
        closedir($handle);
    }
    return $files;
}
My directory structure is

Code: Select all

- Defined
  - dir1
    * file1
    * file2
    * file3
    * etc
  - dir2
    * file1
    * file2
    * file3
    * etc
  - dir3
    * file1
    * file2
    * file3
    * etc
  - dir4
    * file1
    * file2
    * file3
    * etc
  - dir5
    * file1
    * file2
    * file3
    * etc
Now there is a couple of problems that are appearing.

When I pass "defined" as the directory...

1. Every file in dir1 - dir5 gets included twice for some reason.
2. If I pass a lower level directory (ie. dir5), it still for some reason processes higher level directories and includes them in the array (so it still processes dir4, dir3, etc as well).

EDIT: I have found that even if I pass nothing to the function ie. read_dir_into_array()it still process the directories, and thus if i do pass a directory to it, then it process that directory again and adds it to the array, thus including it twice, any ideas why?? thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd use is_dir() to check if a given path is a directory.

Nothing jumps out as to why it's adding duplicates or searching upper levels.. other than maybe $files may be at fault..

edit: sounds like you are accidentally calling it already. Do some debug echo outputs to see when and where it's being called from: http://php.net/debug_backtrace
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Post by SBro »

Yeah thanks Feyd, I discovered that to after my previous edit, doing some debugging now, thanks.
Post Reply