Page 1 of 1

Reading contents of a directory, but not some files?

Posted: Wed Jan 03, 2007 8:46 pm
by Dale
Ok, so listing the contents of a directory is usually with this:

Code: Select all

<?php
$image_folder = "content/directory";
$handle = opendir($image_folder);
while($entry = readdir($handle))
{
if( ($entry != "." || $entry != ".."))AND(ereg(".jpg",$entry)>0) )
echo "$entry<br>";
}
closedir($handle);
?>
Now that lists all the files that end in .jpg however the images, which are listed in the same directory, are named like so: image1.jpg, image1thumb.jpg, image2.jpg, image2thumb.jpg, etc..

What's the easiest way to get it to only list the filenames that are image1thumb.jpg, image2thumb.jpg, etc... ??

:) Thanks :)

Posted: Wed Jan 03, 2007 8:50 pm
by feyd
Your call to ereg() may be modified to include "thumb". It should be noted that the "." in that pattern is understood to be any character, no just a dot. Also "jpg" can exist anywhere in the filename for it to match at this point.

I would suggest using preg_match() and reading up on writing regular expressions from the stickies d11wtq has posted.

Posted: Wed Jan 03, 2007 9:18 pm
by volka

Code: Select all

glob('*thumb.jpg')

Posted: Thu Jan 04, 2007 12:21 am
by John Cartwright
volka wrote:

Code: Select all

glob('*thumb.jpg')
It should be noted that this is case sensitive..

Code: Select all

glob('*thumb.{jpg,JPG}', GLOB_BRACE);
seems more suitable in this case