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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts

    How to count all files on my website?

    Is there a PHP script that can count the total number of files on my website? I need to find out how many files are on my site.

    Thanks...
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #2
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    If you are using php5, you could try something like this;

    Code:
    <?php
    
    $count = sizeof(scandir('./'));
    
    echo 'There are ' . $count . ' files in this directory on my website.';
    
    ?>
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That will work for any single directory.
    Are you asking how to do it over multiple sub-directories as well?
    Edit:
    Oh yeah, and do you count a directory as a file or exclude the count?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    Yes, across multiple sub-directories...
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You didn't mention if you're using PHP5 or not, but thats ok.
    Try this:
    PHP Code:
    $dir getcwd();
    printf("Files in directory %s: %d\n"realpath($dir), countDirectoryFiles($dir));


    function 
    countDirectoryFiles($scanDir null)
    {
        
    $iFileCount 0;

        if (
    is_file($scanDir))
        {
            ++
    $iFileCount;
        }
        else if (
    is_dir($scanDir))
        {
            if (
    false !== ($dh opendir($scanDir)))
            {
                while (
    false !== ($file readdir($dh)))
                {
                    if (
    $file != '.' && $file != '..')
                    {
                        
    $iFileCount += countDirectoryFiles($scanDir DIRECTORY_SEPARATOR $file);
                    }
                }
                
    closedir($dh);
            }
        }
        
        return 
    $iFileCount;

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • The Following 2 Users Say Thank You to Fou-Lu For This Useful Post:

    rfresh (03-02-2009), sashkat (03-28-2009)

  • #6
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    Tha worked perfectly...thanks...
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #7
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Is it possible to modify this script so that it counts the folders in a particular directory, the counts jpg files in only one sub folder in the same directory?

    I'm still new to php

    Thank you


  •  

    Posting Permissions

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