tags:

views:

478

answers:

3

Hello,

function foldersize($path) {

    $total_size = 0;
    $files = scandir($path);


    foreach($files as $t) {

        if (is_dir(rtrim($path, '/') . '/' . $t)) {

            if ($t<>"." && $t<>"..") {

                $size = foldersize(rtrim($path, '/') . '/' . $t);

                $total_size += $size;
            }
        } else {

            $size = filesize(rtrim($path, '/') . '/' . $t);

            $total_size += $size;
        }   
    }

    return $total_size;
}

function format_size($size) {

    $mod = 1024;

    $units = explode(' ','B KB MB GB TB PB');

    for ($i = 0; $size > $mod; $i++) {

        $size /= $mod;
    }

    return round($size, 2) . ' ' . $units[$i];

}

$SIZE_LIMIT = 5368709120; // 5 GB

$sql="select * from users order by id";
$result=mysql_query($sql);

while($row=mysql_fetch_array($result)) {

    $disk_used = foldersize("C:/xampp/htdocs/freehosting/".$row['name']);

    $disk_remaining = $SIZE_LIMIT - $disk_used;
    print 'Name: ' . $row['name'] . '<br>';

    print 'diskspace used: ' . format_size($disk_used) . '<br>';

    print 'diskspace left: ' . format_size($disk_remaining) . '<br><hr>';

}

http://stackoverflow.com/questions/466140/php-disktotalspace#466268

Any idea why the processor usage shoot up too high or 100% till the script execution is finish ? Can anything be done to optimize it? or is there any other alternative way to check folder and folders inside it size?

Thanks.

+1  A: 

There are several things you could do to optimise the script - but maximum success would make it IO-bound rather than CPU-bound:

  1. Calculate rtrim($path, '/') outside the loop.
  2. make if ($t<>"." && $t<>"..") the outer test - it doesn't need to stat the path
  3. Calculate rtrim($path, '/') . '/' . $t once per loop - inside 2) and taking 1) into account.
  4. Calculate explode(' ','B KB MB GB TB PB'); once rather than each call?
Douglas Leeder
+1  A: 

The following are other solutions offered elsewhere:

If on a Windows Host:

<?
    $f = 'f:/www/docs';
    $obj = new COM ( 'scripting.filesystemobject' );
    if ( is_object ( $obj ) )
    {
     $ref = $obj->getfolder ( $f );
     echo 'Directory: ' . $f . ' => Size: ' . $ref->size;
     $obj = null;
    }
    else
    {
     echo 'can not create object';
    }
?>

Else, if on a Linux Host:

<?
    $f = './path/directory';
    $io = popen ( '/usr/bin/du -sk ' . $f, 'r' );
    $size = fgets ( $io, 4096);
    $size = substr ( $size, 0, strpos ( $size, ' ' ) );
    pclose ( $io );
    echo 'Directory: ' . $f . ' => Size: ' . $size;
?>
Jonathan Sampson
A: 

Thanks Johantan Sampson.