Yes, yet another newbie question. In PHP I an get the size of a file/image but how can I obtain the size of all folders and files within in an entire branch of a directory tree?

In other words:

folder1
|folder2
|
folder3
| |folder4
|
folder5

What would the contents total size for folder1 be? How do I calculate that without knowing the tree structure?

Thank you so much for your help on this problem.

😉

    this might be a good foundation to work off of.. this was an old sitemap function i found and just made it add the dir name to $sizes, and attempt to add the file sizes as its value..

    run it and you should know what i mean- its not 100% accurate, and i dont wanna play with it , im tired 🙂

    function dirsize($dir){
    	$sizes=array();
    	$handle=opendir($dir);
    	while($file=readdir($handle))
    		$retVal[]=$file;
    	closedir($handle);
    	sort($retVal);
    
    foreach($retVal as $k=>$v)
    {
    	if($v!="." && $v!=".."):
    		if(is_dir("$dir/$v"))
    			dirsize("$dir/$v");
    		else
    			if(!in_array($dir,$sizes))
    				@$sizes[$dir]+=filesize("$dir/$v");
    	endif;
    }
    print_r($sizes);
    }
    dirsize(".");
    
    

    it was off by maybe a hundred or so bytes.. to get the kb just add in a line like @$sizes+=(filesize("$dir/$v")/1024);

      Thank you for the script, it has been educational but what is the link that I am missing here, I do not see how I could tally up the total bytes so that I could display a content total.

      I see that the values are put into an array, but yet I don't see how to also keep a sum total.

      The accuracy is not critical as long as it is close. I just want to ensure that specified directory (all contents) don't go over an approximate value.

      So, if I am correct I need to keep a running tally of each but I don't quite honestly know how.

      Thank you much for all your other advice, it has been fantastic!!!!

        Okay, the output is (in my case):

        Array ( [./files/sigs] => 129.30078125 ) Array ( [./files/temp] => 30.9736328125 ) Array ( [./files] => 20.1044921875 ) Array ( [./sigs/temp] => 7.12890625 ) Array ( [./sigs] => 167.689453125 ) Array ( [.] => 76.4501953125 )

        I see that the totals (in kbytes) for each subdir is, for example, 129, 30, 20, 7, 167, and 76 respectively.

        I am still unclear on how to add these values into one single variable so that I can then show disk uage for the entire sub-domain user.

        I don't know why I find arrays so baffling, but honestly I can see the solution but not quite get there (newbies, what can ya do?).

        Thanks for all your help.

          maybe something like

          foreach($sizes as $k=>$v)
          {
           static $tkb=0;
           $tkb+=(int)$v;
          }
          echo "Total dir size for $dir is " . $tkb/1024 . "KB";
          

          ??

          it was giving me funny results, but then again- i have a huge dir im testing it in 🙂

            Thanks again, I had come up with the following myself which is in the ball park of what you recommend.

            global $diskuse;
            foreach ( $sizes as $value )
            {
            $diskuse += $value;
            }

            ... then I convert to an integer when I display it.

            I try your method, too. I sure do appreciate your help.

            🆒

            ... Brian

              Write a Reply...