19.9.1 Problem
19.9.2 Solution
If your pattern is a regular expression, read each file from the
directory and test the name with preg_match( ):
$d = dir('/tmp') or die($php_errormsg);
while (false !== ($f = $d->read())) {
// only match alphabetic names
if (preg_match('/^[a-zA-Z]+$/',$f)) {
print "$f\n";
}
}
$d->close();
19.9.3 Discussion
If your pattern is a shell glob (e.g., *.*), use the backtick
operator with ls (Unix) or dir (Windows) to get the matching filenames. For Unix:
$files = explode("\n",`ls -1 *.gif`);
foreach ($files as $file) {
print "$b\n";
}
$files = explode("\n",`dir /b *.gif`);
foreach ($files as $file) {
print "$b\n";
}