
Originally Posted by
schofieldandwhite
Thanks, that's great

could you PM me your full name, so I can credit it in my source code (I'm a university student, and although this isn't for uni; I'm in the habit of crediting everything that isn't my work :p )
Yeah sure, I'll PM you in a few minutes.
I just had to change it again didn't I! erm, this one counts the hidden files seperately from the rest (though they still all go towards the total size). If I were going to use this myself I'd probably make it into a static class. I'm starting to feel I am posting too many alterations already though, so I won't bother posting any more unless people want me to.
It's a programmers life eh! 
PHP Code:
<?php
function dir_size($dir, &$unit, &$block_size=null)
{
$skip = array('.','..','');
$returnarr = array();
$returnarr['dirs'] = 0;
$returnarr['files'] = 0;
$returnarr['hiddenfiles'] = 0;
$returnarr['bytesondisk'] = 0;
$returnarr['sizeondisk'] = 0;
if( @is_resource($handle=opendir($dir)) ) {
$items = null;
while($items[] = readdir($handle)) {
}
closedir($handle);
foreach ($items as $item) {
if( !in_array($item, $skip) ) {
$item_path = $dir.'/'.$item;
if(is_dir($item_path) ) {
$returnarr['dirs']++;
$dir_ret = dir_size($item_path, $unit, $block_size);
$returnarr['dirs'] += $dir_ret['dirs'];
$returnarr['files'] += $dir_ret['files'];
$returnarr['hiddenfiles'] += $dir_ret['hiddenfiles'];
$returnarr['bytesondisk'] += $dir_ret['bytesondisk'];
$returnarr['sizeondisk'] += $dir_ret['sizeondisk'];
} else {
$fsize = filesize($item_path);
if(substr($item,0,1) == '.') {
$returnarr['hiddenfiles']++;
} else {
$returnarr['files']++;
}
$returnarr['bytesondisk'] += $fsize;
if($block_size) {
$blocks = round(($fsize/$block_size)+.5,0) ;
$returnarr['sizeondisk'] += convert_bytes_to(($blocks*$block_size),$unit);
} else {
$returnarr['sizeondisk'] += convert_bytes_to($fsize,$unit);
}
}
}
}
}
return $returnarr;
}
function convert_bytes_to($value, $new_unit)
{
switch( strtolower("$new_unit") )
{
case 'bit':
case 'bits':
return ($value*8);
break;
case 'kb':
case 'kilobyte':
case 'kilobytes':
return $value/1024;
break;
case 'mb':
case 'megabyte':
case 'megabytes':
return $value/1048576;
break;
case 'gb':
case 'gigabyte':
case 'gigabytes':
return $value/1073741824;
break;
}
return $value;
}
$dir = 'D:\mydomain.com\wwwroot\mydir';
$unit = 'mb';
$block_size = 4096;
$total = dir_size($dir,$unit,$block_size);
echo "Total size of $dir = ".$total['sizeondisk']." $unit (".$total['bytesondisk']." bytes)<br />";
echo "The directory is made up of ".($total['files']+$total['hiddenfiles'])." files (".$total['files']." normal and ".$total['hiddenfiles']." hidden) and ".$total['dirs']." folders.<br />";
?>
Bookmarks