You are here: Re: My readdir and display images snippet - Thanks « PHP Programming Language « IT news, forums, messages
Re: My readdir and display images snippet - Thanks

Posted by Steve on 09/13/07 14:37

"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:
> <?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. ;^)

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация