|
Posted by Steve on 09/13/07 16:07
"Confused but working on it" <PostInGroups@wherever.com> wrote in message
news:2007091209500416807-PostInGroups@wherevercom...
> Just wanted to say thanks for the posts helping me make ths work. Added a
> few comments and after the readdir used a pattern match to test for .jpg,
> and seems to work fine on my test setup. Maybe I should test for gif,
> tiff, and png... Anyway, here's the code:
ok, so i've got time on my hands...after re-reading posts today, i'm on this
one again. here's something that will allow you to list any file you want
either filtering by extension or just getting everything. i've been using
the function below since glob became availabe in php. beneath it, i wrapped
up some pseudo-testing code so you could see how the function can be called
and what each param will generate for you.
have fun. ;^)
<?
function listFiles($path, $extension = array(), $combine = false)
{
if (!chdir($path)){ return array(); }
if (!$extension){ $extension = array('*'); }
if (!is_array($extension)){ $extension = array($extension); }
$extensions = '*.{' . implode(',', $extension) . '}';
$files = glob($extensions, GLOB_BRACE);
if (!$files){ return array(); }
$list = array();
foreach ($files as $file)
{
$list[] = ($combine ? $path : '') . $file;
}
return $list;
}
// sampling output
function beautify($html)
{
$html = str_replace(' :: Array', '', $html);
return $html;
}
ob_start('beautify');
// end sample
// test the function
$path = 'c:/inetpub/wwwroot/images';
$extensions = array('jpg', 'gif', 'tif?');
echo '<pre>directory listing for "' . $path . '"</pre>';
$images = listFiles($path);
echo '<pre>no filter, no directory :: ' . print_r($images, true) . '</pre>';
$images = listFiles($path, null, true);
echo '<pre>no filter :: ' . print_r($images, true) . '</pre>';
$images = listFiles($path, 'jpg', true);
echo '<pre>single filter :: ' . print_r($images, true) . '</pre>';
$images = listFiles($path, $extensions, true);
echo '<pre>multiple filter :: ' . print_r($images, true) . '</pre>';
?>
Navigation:
[Reply to this message]
|