Counting Files Script?

Discussion in 'PHP' started by Maverick88, Oct 27, 2007.

  1. #1
    Hi,

    I am looking for a script or piece of code that counts the images I have in a folder including the images in the subfolders.

    I hope someone can help me.
    Thanks in advance.
     
    Maverick88, Oct 27, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Use the script I wrote here: http://forums.digitalpoint.com/showthread.php?t=529171

    Together with this:

    
    $dir = 'path/to/files';
    $files = preg_grep('~\.(jpe?g|bmp|png|gif)$~i', dir_get_contents($dir));
    $num_files = sizeof($files);
    
    echo "There are {$num_files} images.";
    
    PHP:
     
    nico_swd, Oct 27, 2007 IP
  3. Maverick88

    Maverick88 Peon

    Messages:
    272
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Wow, that was fast ! Thanks a lot nico_swd !
     
    Maverick88, Oct 27, 2007 IP
  4. Maverick88

    Maverick88 Peon

    Messages:
    272
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Could you please explain me this piece of code:

     
    Maverick88, Oct 27, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    If the current entry is a file, it'll be added to the files array (which will be returned at the end). And if the current entry is a directory, the function will call itself again and read the files from this directory and add them to the array as well.

    Oh, and please use [php][/php] tags when posting PHP code.
     
    nico_swd, Oct 27, 2007 IP