|
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
|
Improved Version:
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.
|
|
|
|
|