how to show how many files are in a directory

Discussion in 'PHP' started by Joseph, Feb 18, 2006.

  1. #1
    Does anyone have the code to display how many files are in a folder on your website?

    Example if i stored my images in a folder called images:

    We currently host: 80 photos

    Any idea how to do this?

    Thanks

    Joseph
     
    Joseph, Feb 18, 2006 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    digitalpoint, Feb 18, 2006 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    
    <?php
    //directory path
    $dir = $_SERVER['DOCUMENT_ROOT'].dirname($PHP_SELF); 
    
    //open the directory
    $open_dir = opendir($dir);
    
    //reset the counter
    $count = 0;
    
    //loop through the directory
    while (false !== ($file = readdir($open_dir))) {
    
    //count files but remove .. and .
    if (is_file($file) && $file !== '.' && $file !== '..') {
    $count++;
    
    }
    }
    
    echo $count;
    ?>
    
    PHP:
    This should do it for you as well.
     
    jestep, Feb 18, 2006 IP
  4. Joseph

    Joseph Well-Known Member

    Messages:
    886
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    130
    #4
    thanks guys :)
     
    Joseph, Feb 19, 2006 IP