Reading contents of a directory, but not some files?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Reading contents of a directory, but not some files?

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

glob('*thumb.jpg')
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
Post Reply