Current location: Hot Scripts Forums » Programming Languages » PHP » Just cant get it to show all dirs size


Just cant get it to show all dirs size

Reply
  #1 (permalink)  
Old 03-19-07, 02:13 AM
presso presso is offline
New Member
 
Join Date: Mar 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy Just cant get it to show all dirs size

Hi all new to this site , I am having a few problems with the code that will follow this , The code currently works correctly for what it is ment to do grab the set dirs size and amount of files and amout of dirs off the set dir , What i am trying to do is get it to show all of the above but include all the child dirs that are off all the others , In other words what i would like to do via php is show a total of all the files and dirs and the total all added up of file size which would return a complete size, What i am trying to do is return all these values so i can show a complete site size. Hope i have not lost everybody , following is the working code for just the set dir.

PHP Code:

$path="you can set this to your dir";

$totalsize 0;
  
$totalcount 0;
  
$dircount 0;
  if (
$handle opendir ($path))
  {
    while (
false !== ($file readdir($handle)))
    {
      
$nextpath $path '/' $file;
      if (
$file != '.' && $file != '..' && !is_link ($nextpath))
      {
        if (
is_dir ($nextpath))
        {
          
$dircount++;
          
$result = ($nextpath);
          
$totalsize += $result['size'];
          
$totalcount += $result['count'];
          
$dircount += $result['dircount'];
        }
        elseif (
is_file ($nextpath))
        {
          
$totalsize += filesize ($nextpath);
          
$totalcount++;
        }
      }
    }
  }
  
closedir ($handle); 
Reply With Quote
  #2 (permalink)  
Old 03-19-07, 04:56 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
what you need is recursion, this is a function that will call itself when a certain check returns true or false.
Atm, your script will only loop over the first dir, and then stop. If you want it to loop over all subdirs as well, you will have to keep looping over every dir, and since you don't know how many dirs there are on your folder, you don't know how many while loops you will have to write.

Here's a script (just a copy of yours, but slightly changed), tha will loop over every subdir, and will store the number of files and the number of sizes:
PHP Code:

$path="you can set this to your dir";
$totalsize 0;
$totalcount 0;
$dircount 0;
function 
dirscanner($path)
{
  global 
$dircount$totalsize$totalcount;
  if (
$handle opendir ($path))
  {
    while (
false !== ($file readdir($handle)))
    {
      
$nextpath $path '/' $file;
      if (
$file != '.' && $file != '..' && !is_link ($nextpath))
      {
        if (
is_dir ($nextpath))
        {
          
dirscanner($nextpath);
          
$totalcount++;
          
$dircount++;
        }
        elseif (
is_file ($nextpath))
        {
          
$totalsize += filesize ($nextpath);
          
$totalcount++;
        }
      }
    }
  }
}
// now call the function:
dirscanner($path);
// close the dir-handler
closedir ($handle);
// output the values:
echo "totalsize: ".$totalsize." - totalcount: ".$totalcount." - dircount: "+$dircount
UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #3 (permalink)  
Old 03-19-07, 05:25 AM
presso presso is offline
New Member
 
Join Date: Mar 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
thanks for having a look at this , I have had a play around with your mods but i only seem to be getting an error of.

PHP Code:

Warningclosedir(): supplied argument is not a valid Directory resource in /home/myusername/public_html/sitesize.php on line 40 

line 40 of course would be

PHP Code:

closedir ($handle); 

any ideas on why this might be ??
Reply With Quote
  #4 (permalink)  
Old 03-19-07, 06:32 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
yup, just a small mistake of me:
PHP Code:

$path="you can set this to your dir";
$totalsize 0;
$totalcount 0;
$dircount 0;
function 
dirscanner($path)
{
  global 
$dircount$totalsize$totalcount;
  if (
$handle opendir ($path))
  {
    while (
false !== ($file readdir($handle)))
    {
      
$nextpath $path '/' $file;
      if (
$file != '.' && $file != '..' && !is_link ($nextpath))
      {
        if (
is_dir ($nextpath))
        {
          
dirscanner($nextpath);
          
$totalcount++;
          
$dircount++;
        }
        elseif (
is_file ($nextpath))
        {
          
$totalsize += filesize ($nextpath);
          
$totalcount++;
        }
      }
    }
  }
  
// close the dir-handler
  
closedir ($handle);
}
// now call the function:
dirscanner($path);
// output the values:
echo "totalsize: ".$totalsize." - totalcount: ".$totalcount." - dircount: "+$dircount
this should do. I didn't test the previous one, so there might still be some errors

UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #5 (permalink)  
Old 03-19-07, 02:51 PM
presso presso is offline
New Member
 
Join Date: Mar 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
cheers it got rid of the errors but is returning values of zero , thats ok its something i can work on , What i am wondering though is is this possible to do without having to do it via a function , I am trying to avoid doing it via a function as this snippet of code is already going to be part of another function. thats why the first bit of code i posted is not a function as its comming out of a function that i am already using. But i would like it to be apart of this function and not a complete seperate one.
Reply With Quote
  #6 (permalink)  
Old 03-19-07, 03:42 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Yeah, keep with the functions. Much easier to organize.

Here's something I've been using for a while now. Just point it at a folder and it will return the total size in bytes.
PHP Code:

<?php

/**
 * dirsize_recursive()
 * Scans a directory and all it's contents to get the total size
 * 
 * @param    string   $directory     The path to the directory
 * @return   int                     SUCCESS: Total directory size in bytes; FAIL: -1
 */
function dirsize_recursive$directory )
{
    while ( 
substr$directory, -) == '/' )
        
$directory substr$directory0, -);
    if ( !
file_exists$directory ) OR !is_dir$directory ) OR !is_readable$directory ) )
        return -
1;
    
$size 0;
    
$dir  opendir$directory );
    while ( ( 
$file readdir$dir ) ) !== FALSE )
    {
        if ( 
$file != '.' AND $file != '..' )
        {
            
$path $directory '/' $file;
            if ( 
is_readable$path ) )
            {
                if ( 
is_file$path ) )
                    
$size += filesize$path );
                else if ( 
is_dir$path ) )
                {
                    
$dirsize dirsize_recursive$path );
                    if ( 
$dirsize >= )
                        
$size += $dirsize;
                    else
                        return -
1;
                }
            }
        }
    }
    
closedir$dir ); 
    return 
$size;
}

?>
PHP Code:

$dirsize dirsize_recursive'./files' ); 

__________________
The toxic ZCE

Last edited by Keith; 03-19-07 at 03:44 PM.
Reply With Quote
  #7 (permalink)  
Old 03-19-07, 04:54 PM
presso presso is offline
New Member
 
Join Date: Mar 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks keith that works very well , Would you be able to mod it to show how many files and dirs there are as well.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Check uploaded files size zoliky PHP 5 09-27-06 01:52 PM
One click to make text size bigger for readers iKwak HTML/XHTML/XML 1 10-15-04 07:16 PM
limiting size of pop-up window on Mac magda JavaScript 0 05-10-04 08:28 PM
openNewWindow windows all the same size? ChrisPozzi JavaScript 2 04-01-04 12:00 PM
Short Command Line Based Item Order Program digioz C/C++ 20 12-27-03 08:17 PM


All times are GMT -5. The time now is 05:58 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.