A Better Version with More Options

This version has added the FullPath and DirSep parameters allowing full control over the type of list that is returned

Code
function Dir_FileList ($Dir, $Match = null, $ObjectType = 0, $FullPath = false, $DirSep = null, $Suffix = '') {
    /*
    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
        FullPath (Optional Boolean/String) = Determines if the returned list contains only file/directory names or complete paths.
            If (boolean) false, only file/directory names are returned.
            If (boolean) true, complete paths are returned. Complete paths never contain trailing slashes.
            If a string, then this is prepended to each returned file/directory.
                This enables the easy transposition of a list of files/directories.
                When using this feature, remember to us a trailing slash to separate this string and the returned file/directory.
            Default: false
        DirSep (Optional String) = The directory separator to use.
            When returning full paths (FullPath = true), this is the directory separater used between the specified 
                directory (Dir) and each file/directory returned.
            This is also the directory separator that is expected to be present in Dir.
            This should normally be "/" on *nix systems and "\" on Windows systems, but does not have to be as Windows can
                also use "/" as a directory separator.
            This may contain zero-or-more characters.
            If null, this defaults to the current value of DIRECTORY_SEPARATOR.
            Default: null
        Suffix (Optional String) = A suffix that is appended to each element of the returned array.
            This enables easy customization of the returned list of files/directories.
            When using this feature, you will normally want to use a leading slash.
            Default: "" (zero-length string)
    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.
        1.3 2013.03.29  Added the FullPath, DirSep, and Suffix parameters.
    */
    $r = array();

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

    // Default parameters.
    if (is_null($DirSep)) $DirSep = DIRECTORY_SEPARATOR;
    if (is_string($FullPath)) $PrefixPath = $FullPath;
    else {
        if ($FullPath) $PrefixPath = $Dir . $DirSep;
        else $PrefixPath = '';
    }

    // 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.$DirSep.$FileName)) ||
                    ($ObjectType == 2 && is_dir($Dir.$DirSep.$FileName))
                )
            ) {

            // No match filter specified. Accumulate all files.
            if (is_null($Match)) $r[] = $PrefixPath . $FileName . $Suffix;
            // 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
                // The regexp matches the file name, append it to the return buffer
                if ($x == 1) $r[] = $PrefixPath . $FileName . $Suffix;
            }
        }
    }

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

}

Last edited by Russ; 03/29/13 03:34 AM.

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)