Improved Version:

Code

    function Dir_FileList ($Dir, $Match = null) {
        /*
        Purpose
            Used to get a list of files 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 names.
                Only files matching this regular expression are returned.
                If null, all files in the specified directory are returned.
                Default: null
        Return (Array)
            Returns a list of all files in the specified directory (Dir) that match the specified expression (Match).
            Directories are not returned.
            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 = Files_GetList('/home');
                Returns all files in the /home directory.
            $x = Files_GetList('/home', '/.*\.txt$/');
                Returns all files 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.
        */

        $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 != '..' && is_file($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/29/12 02:06 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)