Guardian-Mage Posted July 25, 2007 Share Posted July 25, 2007 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; ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted July 25, 2007 Share Posted July 25, 2007 function countfiles($path, $filter) { $dir = opendir($path); while ($file = readdir($dir)){ if(strstr($file, $filter)){ $i++; } }//end while closedir($dir); return $i; } Quote Link to comment Share on other sites More sharing options...
Guardian-Mage Posted July 25, 2007 Author Share Posted July 25, 2007 is filter the type of file because I need to count all files Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 25, 2007 Share Posted July 25, 2007 have you looked at the glob() function? Quote Link to comment Share on other sites More sharing options...
Guardian-Mage Posted July 25, 2007 Author Share Posted July 25, 2007 I couldn't get it to work Quote Link to comment Share on other sites More sharing options...
wmbetts Posted July 25, 2007 Share Posted July 25, 2007 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); ?> Quote Link to comment Share on other sites More sharing options...
wmbetts Posted July 25, 2007 Share Posted July 25, 2007 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()); ?> Quote Link to comment Share on other sites More sharing options...
keeB Posted July 25, 2007 Share Posted July 25, 2007 Yeah but that doesn't get sub directories/files.. I'll provide a function in a bit, if I remember Quote Link to comment Share on other sites More sharing options...
Guardian-Mage Posted July 25, 2007 Author Share Posted July 25, 2007 That works, but does not provide the number of files/directories and I cannot find a way to do this Quote Link to comment Share on other sites More sharing options...
wmbetts Posted July 25, 2007 Share Posted July 25, 2007 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 Quote Link to comment Share on other sites More sharing options...
wmbetts Posted July 25, 2007 Share Posted July 25, 2007 <?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 Quote Link to comment Share on other sites More sharing options...
wmbetts Posted July 25, 2007 Share Posted July 25, 2007 Yeah but that doesn't get sub directories/files.. I'll provide a function in a bit, if I remember Yes the second one does. It's a recursive function. Quote Link to comment Share on other sites More sharing options...
keeB Posted July 25, 2007 Share Posted July 25, 2007 Ah I didn't see the recursive bit Sorry Quote Link to comment Share on other sites More sharing options...
Guardian-Mage Posted July 26, 2007 Author Share Posted July 26, 2007 I need to write out the TOTAL number of FILES and SUB-DIRECTORIES Thanks Quote Link to comment Share on other sites More sharing options...
Guardian-Mage Posted July 26, 2007 Author Share Posted July 26, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.