Afficher un message
Vieux 13/09/2007, 16h37   #2
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: My readdir and display images snippet - Thanks


"Confused but working on it" <PostInGroups@wherever.com> wrote in message
news:2007091209500416807-PostInGroups@wherevercom...
> Just wanted to say thanks for the posts ing 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:
> <?php
> //Open images directory
> $dir = opendir("images");
> //read files in the dir
> while (($file = readdir($dir)) !== false)
> //test to make sure jpg
> if (eregi("\.jpg",$file))


one suggestion,

why are you using ereg? beside the fact that it is vastly more resource
intensive than preg and slower, neither is needed because substr is faster
than both.

while ...
$extension = strtolower(substr($file, -4));
if ($extension != '.jpg'){ continue; }
whatever you want to do with the jpg from here
end

you could also use fnmatch to pattern match for file names. you could even
use glob()...

$directory = 'images';
$extension = '*.jpg';
chdir($directory);
foreach (glob($extension) as $image)
{
echo '<img src="' . $directory . '/' . $image . '" class="pad1em">';
}

i personally prefer glob() for such things since it requires less code to
get what you want.

anyway...there's always more than one way to skin a cat. ;^)


  Réponse avec citation
 
Page generated in 0,06200 seconds with 9 queries