Jump to content

[SOLVED] Count all files in a directory and subdirectory


Guardian-Mage

Recommended Posts

I need to count all files in the given directory and all subdirectories and count all sub-directories. How might I do this. My current code counts all files and in the specified directory but doesn't list the ones in sub-directories.

 

<?php
$directory = opendir("NLT V2/");
while($item = readdir($directory)){
if(($item != ".") && ($item != "..")){
$files[] = $item;
echo "<a href=\"NLT V2/$item\">$item</a><br />";
}
}
$sizeofarray = count($files);
echo $sizeofarray;
?>

Link to comment
Share on other sites

This doesn't tell you what files are in what directories, but it will count all directories and files. This ignores .. and .

 

diectory listing (2 directories and 5 files):

 

server1# ls -al
total 18
drwxr-xr-x   4 root  will   512 Jul 25 15:50 .
drwxr-xr-x  47 will  will  3072 Jul 25 11:00 ..
-rw-r--r--   1 root  will   408 Jul 25 15:41 array.php
drwxr-xr-x   2 root  will   512 Jul 25 15:10 core
-rw-r--r--   1 root  will   516 Jul 25 16:09 count.php
-rw-r--r--   1 root  will  1571 Jul 25 15:01 main.php
drwxr-xr-x   2 root  will   512 Jul 25 15:08 menus
-rw-r--r--   1 root  will   222 Jul 25 15:27 time.php       
server1# ls core/
core.php

server1# ls menus/

server1# 

 

output:

 

server1# php count.php 
Array
(
    [dir] => 2
    [file] => 5
)
server1# 

 

code:

 

<?php


function count_stuff($dir)
{

        static $arr = Array("dir" => "0" , "file" => "0");


        if (($dh = @opendir($dir)) !== false) {
                while ($file = readdir($dh)) {
                        if (is_dir($file)) {
                                if (strstr($file,".") || strstr($file,"..")) {
                                        continue;
                                } else {
                                        $arr["dir"] += 1;
                                        count_stuff($file);
                                }
                        } else {
                                $arr["file"] += 1;
                        }

                }
        } else {
                die ("Directory supplied is not a directory.\n");
        }

        return $arr;
}

$a = count_stuff("/home/will/php-mtg/");
print_r($a);
?>

Link to comment
Share on other sites

I should have tested that better.... =\ sorry Here's a better version.. why is it better? it's smaller and actually works right.

 

<?php

function get_list($dir)
{

        foreach(glob("${dir}/*") as $fn) {
                if (is_dir($fn)) {
                        get_list($fn);
                } else {
                        print $fn . "\n";
                }
        }
}

get_list(getcwd());
?>

 

 

 

Link to comment
Share on other sites

That works, but does not provide the number of files/directories and I cannot find a way to do this

 

do you want total number of files and directories or total number of files in corresponding directories?

 

/dir  2 files

/dir/subdir1  3 files

 

or

Files: 100

Dir: 50

Link to comment
Share on other sites


<?php
function get_list($dir)
{

        static $arr = Array();

        if (!array_key_exists($dir,$arr)) {
                $arr[$dir] = 0;
        }

        foreach(glob("${dir}/*") as $fn) {
                if (is_dir($fn)) {
                        get_list($fn);
                } else {
                        $arr[$dir] += 1;
                        print $fn . "\n";
                }
        }
        return $arr;
}

$a = get_list(getcwd());
print "\n\n";
foreach($a as $k => $v) {
        print "Number of files in ${k}: ${v} \n";
}
?>

 

it will product output like :

 

server1# php a.php
/usr/home/will/php-mtg/a.php
/usr/home/will/php-mtg/array.php
/usr/home/will/php-mtg/core/a/a.php
/usr/home/will/php-mtg/core/core.php
/usr/home/will/php-mtg/count.php
/usr/home/will/php-mtg/main.php
/usr/home/will/php-mtg/menus/a/a/b.php
/usr/home/will/php-mtg/menus/a/a.php
/usr/home/will/php-mtg/menus/a/b.php
/usr/home/will/php-mtg/time.php
/usr/home/will/php-mtg/time1.php
/usr/home/will/php-mtg/time2.php


Number of files in /usr/home/will/php-mtg: 7 
Number of files in /usr/home/will/php-mtg/core: 1 
Number of files in /usr/home/will/php-mtg/core/a: 1 
Number of files in /usr/home/will/php-mtg/menus: 0 
Number of files in /usr/home/will/php-mtg/menus/a: 2 
Number of files in /usr/home/will/php-mtg/menus/a/a: 1 


Link to comment
Share on other sites

I have done it, if any one wants it here is the script

<?php
$myvar = 0;
$myvar2 = 0;

function get_list($dir)
{
global $myvar,$myvar2;
        foreach(glob("${dir}/*") as $fn) 
	{
                if (is_dir($fn)) 
			{
                        get_list($fn);
					$myvar2++;
                } else 
			{
                        //print $fn . "<br />";
					$myvar++;
                }
        }
}

get_list('Dir');
echo "$myvar<br />";
echo $myvar2;
?>

 

at the bottom replace Dir with the appropriate directory

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.