Web Design Blog

This is where we store some of our Web Development thoughts, tips and tricks, just because we like to share.

PHP- Count Files In A Directory

I’ve just added a new feature to the Team page, where i’ve added a small CSS image gallery. So now you can see multiple images of the awesome BrightCherryteam!

I like everything to be automated so there is very little input from a human to make things work. To help make the thumbnail image gallery completely automated and dynamic, I needed to get a count of how many images there are in each member’s image folder.

Count files

So for example, if Harry our Sales Director has 2 images of himself in his images folder, our script will automatically pull out 2 images. If he uploads a new image of himself in his folder, our script will automatically pull out 3 images. In order for that to work, I needed to write a line of PHP code that counts how many image files there are in his image folder each time the page loads.

I’ve seen plenty of PHP code out there that does a file count in a directory. To be honest, most of the code i’ve seen has been bloated- code that is far too long. All you really need is 1 line of code to do a file count.

Here’s the code I used to count how many files there are in a directory:

1
2
3
4
5
6
7
8
9
10
$directory = "../images/team/harry/";
if (glob($directory . "*.jpg") != false)
{
 $filecount = count(glob($directory . "*.jpg"));
 echo $filecount;
}
else
{
 echo 0;
}

In my particular example, it’s only counting files with a jpg extension. You can change that to anything (i.e txt), or completely remove it if you want to count all files. The * is acting as a wildcard.

In the following few days I’ll provide the code for the actual CSS image gallery.

06 Sep 2008 / 22 Comments / PHP Scripts, Tips & Tricks / by Maruf

22 Comments

  1. PoZel
    06/11/2008
    1

    great! nice and simple. thanks!!

  2. Krista Ehlers
    10/02/2009
    2

    I love this! I thought the other code (mostly loops that increment a counter) was bloated, too. Thanks!

  3. Nike
    18/04/2009
    3

    Thanks a lot. Finally a piece of PHP that actually works.

    Great job. :)

  4. humble
    16/09/2009
    4

    Thank you very much.. Works great. :)

  5. shiva
    13/01/2010
    5

    Masha Allah …lovely code Thanks …Maruf

  6. Pim
    03/02/2010
    6

    Thanks for the code. However I found that when there are 0 files it still returns 1. I fixed it with an if statement:


    //Count .jpg

    if (glob(“” . $directory . “*.jpg”) != false)
    $filecount = count(glob(“” . $directory . “*.jpg”));
    else
    $filecount = 0;

    Maybe this helps some people struggeling with the same issue.

  7. Solomon
    18/02/2010
    7

    Wow! I can’t count the number of times I’ve been digging around the internet for the same old “bloated” script because I’ve never actually bothered leaning it by heart. If I’d have done my research a little better I would have found this solution years ago! Simple, elegant, and it just plain works!

    I needed the a script to return the filenames too, regardless of their extension. This is my take on it… I hope someone finds it helpful:

    # code to return an array of file paths from a directory
    $filepath = $pathtodirectory . “*”;
    $filenames = array();
    foreach (glob($filepath) as $filename) {
    array_push($filenames , $filename);
    }
    print_r($filenames);

    I learnt that the path can be relative but it shouldn’t have a leading slash. Also the “*” on the end of the filepath catches all files.

  8. Maruf
    18/02/2010
    8

    Hey Soloman,

    I’m not entirely sure why you needed to use array_push there. Regardless, I have already written a post on how you get the files names here: http://www.brightcherry.co.uk/scribbles/2008/11/02/php-list-all-files-in-a-directory/

    :)

  9. Maruf
    18/02/2010
    9

    Hey Pim,

    I’ve actually experienced that problem before.

    Many thanks for sharing your solution :)

    Great stuff!

  10. Sukumar
    02/09/2010
    10

    Thanks a lot for ur help…..!!! Can u pls tell me how to count no of files in a directory of different types?

  11. Peter from Hungary
    22/11/2010
    11

    Thanks very much! This is exactly what I was looking for!
    Great! +++

  12. 19/12/2010
    12

    I am a new in PHP but i try Your code thanks.
    Modified code for directory Listing

    <?php
    $sno = 1;
    echo count(glob('*.*'));
    echo " Files”;
    foreach(glob(‘*.*’) as $file)
    {echo “$sno. $file\n”;$sno++;}
    ?>

  13. 20/01/2011
    13

    Neat bit of code, thanks

  14. 14/03/2011
    14

    Thanks! Really useful :) Just what I was looking for ;)

  15. domenico
    19/04/2011
    15

    Hey guys, because i spent a few hours on this, in case some of you has my same problem (to me it kept returning 0 no matter what), it might be something really stupid such as the space before the *

    // not working (notice the space before the *)
    $numOfPics = count(glob(“” .$dir. ” *.jpg”));

    // working fine
    $numOfPics = count(glob(“” .$dir. “*.jpg”));

    hope this helps someone :)

  16. Anoop Sankar
    26/05/2011
    16

    Nice Code, really helpful!!

  17. Ray
    12/07/2011
    17

    I think this code is doing what I need, but I am not sure how to access the output.

    I am using Flash to play a movieclip of images in a directory. I have the Actionscript set up to randomly generate the file number (i.e. slideshow1.jpg, slideshow3.jpg, etc.) I just need the total number of jpgs in a directory to become the value for “var maxVal = ”

    I think I can put a script in identifying the output of the php as a variable, then set that variable as the value for maxVal so – var maxVal = VARIABLE_NAME.

    Any ideas? Am I even on the right track?

    Thank you!

  18. Bliz
    22/09/2011
    18

    This works perfectly! Thank you!

  19. flav1o
    26/10/2011
    19

    Just a heads up. Remember to use relative path to the folder and not absolute path when using glob.
    Ex

    Working:
    $directory = “whatever/wp-content/uploads/random/”;

    NOT Working
    $directory = “http://mydomain.com/whatever/wp-content/uploads/random/”;

    cheers

  20. Tim
    21/12/2011
    20

    Nice and simple, but anyone know how can we count the sub-directories as well.

  21. Dragster
    27/12/2011
    21

    Hey,
    i edited it to count ALL files in the folder and not pictures
    for those who want this code on a function
    here it is
    you have to define this:

    $directory = ”;
    if (glob($directory . “*.*”) != false)
    {
    $filecount = count(glob($directory . “*.*”));
    }
    else
    {
    $filecount = ’0×1′;
    }

    and add this to your index or what ever! :)

  22. Eman
    21/01/2012
    22

    And if the case is that

    1. having only 1 directory lets suppose Images

    2. Added category and categorized images category wise…

    3. Now want to count the images according to their category then what will be the code for this by counting images from 1 directory but for different categories… plz any one help….

Leave a Reply

© 2012 BrightCherry :)