Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2006
    Location
    New Zealand
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I check the total number of files in a folder?

    I want to use a loop to list the file names in a folder. How do I find out the total number of files in the folder so I know when to end the loop? I can't use 'file exists' because the file names could be anything.

  • #2
    New Coder
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    49
    Thanks
    0
    Thanked 3 Times in 3 Posts
    If you use PHP5, then you can do something like this
    PHP Code:
    <?php

    $file 
    scandir("path/to/your/folder");

    foreach(
    $file as $key => $value) {

        if(
    is_file($value)) {
        
    $total++; // Counter
        
    echo "$value<br />\n";
        }

    }

    echo 
    "<p>Total files: $total</p>";

    ?>

    If you dont use PHP5, then do something like this
    PHP Code:
    <?php

    $dir 
    "path/to/your/folder";

    if (
    $check opendir($dir)) {

        while((
    $file readdir($check)) !== false) {
        
            if (
    is_file($file)) {
            
    $total++; // Counter
            
    echo "$file<br />\n";
            }
        
        }
        
    }

    echo 
    "<p>Total files: $total</p>";

    ?>

  • #3
    New Coder
    Join Date
    May 2004
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I use something like this:

    Code:
    $files = array();
    $dir = opendir('./smily');
    while(($file = readdir($dir)) !== false)
    {
     if($file !== '.' && $file !== '..' && !is_dir($file))
     {
       $files[] = $file;
     }
    }
    closedir($dir);
    sort($files);
    requesting the lenght of the array will get you the number of files.
    and to go through all these files:
    [code]
    for($j=0; $j<(count($files)-1); $j++)
    {
    echo ($files[$j]);
    }
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •