|
1 members (Russ),
4,291
guests, and
724
robots. |
|
Key:
Admin,
Global Mod,
Mod
|
|
|
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
|
|
|
|
|
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.
|
|
|
|
Joined: Dec 1999
Posts: 30,868
Master Elite Member
|
Further Improved VersionNow supports selecting between only directories or only files or both.
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.
|
|
|
|
|