News you won't see in controlled mainstream media.

Who's Online Now
1 members (Russ), 10,950 guests, and 1,516 robots.
Key: Admin, Global Mod, Mod
ShoutChat
Comment Guidelines: Do post respectful and insightful comments. Don't flame, hate, spam.
December
S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Left Sidebar Ad
This is another custom island. This one could be used for ads. This spot is one of the best places for ads on your site to be placed. This can be enabled/disabled on a per forum basis.
Popular Topics(Views)
419,416 DOES GOD EXIST?
324,415 Please HELP!!!
Support Our Forum
Previous Thread
Next Thread
Print Thread
Rate Thread
Joined: Dec 1999
Posts: 30,868
Master Elite Member
****
PHP Code
 function DirList ($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.
		Return (Array)
			Returns a list of all files 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.
		Version History
			1.0 2012.01.01 Tested by Russ Tanner - russtanner.com
		*/

		$r = array();									   // Return buffer

		$hFile = opendir($Dir);							 // Open the specified directory
		if ($hFile === false) return false;				 // Error Trap: The specified dir is invalid, return false

		while ($FileName = readdir($hFile)) {			   // Main loop: Read all files in the specified directory

			if ($FileName != '.' && $FileName != '..') {	// Skip the files "." and ".."
				$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
			}
		}

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

	} 
 


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)
Joined: Dec 1999
Posts: 30,868
Master Elite Member
****
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 01: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)
Joined: Dec 1999
Posts: 30,868
Master Elite Member
****
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 01: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)
Joined: Dec 1999
Posts: 30,868
Master Elite Member
****
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 02: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)

Link Copied to Clipboard
Powered by UBB.threads™ PHP Forum Software 8.0.1