image count in a directory

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
jasondavis
Forum Commoner
Posts: 60
Joined: Sat Feb 04, 2006 5:35 pm

image count in a directory

Post by jasondavis »

I need to show the count of imnages in a specific directory, I have this code below but it only shows
.JPG

I need to add ability to count images with
.jpg
.PNG
.png
.gif
.GIF

any help appreciated

Code: Select all

<?php
$my_jpg_counter=0;
$d = dir($my_dir = "../signs/tobeapproved");
while (false !== ($entry = $d->read())) {
    $ext = strrchr($entry, ".");
    if($ext == ".JPG")
        $my_jpg_counter++;
}
$d->close();

echo "<font color=\"0270CA\" face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"3\"><strong>(<font color=\"000000\">$my_jpg_counter</font>)</strong></font>";
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You could try

Code: Select all

$validExtensions = array('jpg' => 0, 'gif' => 0, 'png' => 0);

foreach (glob($dir.'{*.'. implode(', *.', array_keys($validExtensions)) .'}', GLOB_BRACE) as $fileName) {
	$file = explode('.', $fileName);
	$file = array_reverse($file);
	$validExtensions[$file[0]]++;
}

print_r($validExtensions);
I acutally just wrote nearly the exact same thing :). Enjoy.
jasondavis
Forum Commoner
Posts: 60
Joined: Sat Feb 04, 2006 5:35 pm

Post by jasondavis »

sorry im new how wuld I put that into use?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

A file named whatever.jpeg won't be recognized.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why not use getimagesize() if you're looking for images? At least then, you can verify they are as close as possible to being images as possible...
Post Reply