how to show No of images in 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
kumel619
Forum Newbie
Posts: 1
Joined: Tue Aug 02, 2011 1:33 pm

how to show No of images in directory?

Post by kumel619 »

I need to use php codes to show how many images r dr in particular directory.. so please help me on dat.. ASAP :(
genix2011
Forum Commoner
Posts: 74
Joined: Tue Aug 02, 2011 4:00 pm

Re: how to show No of images in directory?

Post by genix2011 »

You can count the files in a specific directory like that:

Code: Select all

function countFiles($directory){
    $glob = glob($directory . "*.*");
    if ($glob != false) {
        return count($glob);
    }
    return 0;
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: how to show No of images in directory?

Post by pickle »

[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.
In other news, ~genix2011's code will work, but should be tweaked:

1) glob() can return an empty array, a populated array, or boolean FALSE on error. Generally I'd have the empty array and the error condition return different values, so you can tell if the directory is actually empty, or if a problem happened.
2) The function as it stands will return all files/sub-directories in a directory. If you only want images, you need to pass the parameters to glob a bit differently

Code: Select all

function countFiles($directory){
    $glob = glob($directory . "*.{jpg,gif,png,etc}",GLOB_BRACE);
    if ($glob !== false) {
        return count($glob);
    }
    return false;
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply