Further Improved Version

Now supports selecting between only directories or only files or both.

Code
function Dir_FileList ($Dir, $Match = null, $ObjectType = 0) {
    /*
    Purpose
        Used to get a list of files or directories in a directory matching a specified regular expression.
    Parameters
        Dir (String) = The directory we're working with.
        Match (Optional String) = A regular expression used to match file/directory names.
            Only files/directories matching this regular expression are returned.
            If null, all files/directories in the specified directory are returned.
            Default: null
        ObjectType (Optional Integer) = Determine if files or directories (or both) are returned.
            If 0, both files and directories are returned.
            If 1, only files are returned.
            If 2, only directories are returned.
            Default: 0
    Return (Array)
        Returns a list of all files/directories in the specified directory (Dir) that match the specified expression (Match).
        The . and .. files are never included in the returned list.
        Returns false if:
            - The specified directory (Dir) does not exist.
            - The specified regular expression (Match) is invalid.
    Examples
        $x = Dir_FileList('/home');
            Returns all files and directories in the /home directory.
        $x = Dir_FileList('/home', '/.*\.txt$/', 1);
            Returns all files (no directories) in the /home directory that end in ".txt".
    Version History
        1.0 2011        Tested by Russ Tanner on PHP 5.1.6. - russtanner.com
        1.1 2012.03.28  Tested by Russ Tanner on PHP 5.3.10 - russtanner.com
            Allowed omitting Match so that ALL files are returned.
            Limited the resulting list to files only. No directories are returned.
        1.2 2013.03.08  Added ObjectType parameter.
    */

    $r = array();
    
    // Make sure Dir does not have a trailing slash.
    $Dir = rtrim($Dir, '/\\');

    // Open the specified directory.
    $hFile = opendir($Dir);
    // Error Trap: The specified directory (Dir) is invalid. Return false and exit.
    if ($hFile === false) return false;

    // Main loop: Read all files in the specified directory
    while ($FileName = readdir($hFile)) {
        // Skip "." and ".." and any directories.
        if  ($FileName != '.' && $FileName != '..' && 
                (
                    ($ObjectType == 0) || 
                    ($ObjectType == 1 && is_file($Dir.'/'.$FileName)) ||
                    ($ObjectType == 2 && is_dir($Dir.'/'.$FileName))
                )
            ) {
                    
            // No match filter specified. Accumulate all files.
            if (is_null($Match)) $r[] = $FileName;
            // A match filter was specified. Only return files matching the regular expression (Match).
            else {
                $x = preg_match($Match, $FileName);     // Test the current file against the regexp
                if ($x === false) return false;         // Error Trap: The specified regular expression is invalid
                if ($x == 1) $r[] = $FileName;          // The regexp matches the file name, append it to the return buffer
            }
        }
    }

    // Clean up and return the resulting array.
    closedir($hFile);
    return $r;

}


Last edited by Russ; 03/08/13 02:49 PM.

The Captian
Today they call you "crazy". Tomorrow they call you "ahead of your time."
Global Skywatch Learn about Chemtrails - You're breathing them now!
OnlyTheBestHerbs.com World-class supplements
Mercury Talk Why you are sick.
OneUp Domains Domains, Hosting, Email
1-800-358-4278 (U.S. & Canada)