Size Usage

8 Dec

<?php
/*
* Returns disk usage in bytes of directory $d. Limit depth level with $depth.
* Updates, documentation and examples: http://daniel.lorch.cc/projects/disk_usage/
*
* Revision: 1.00
*/

function disk_usage($d, $depth = NULL) {
if(is_file($d))
return filesize($d);

if(isset($depth) && $depth < 0)
return 0;

if($d[strlen($d)-1] != '\\' || $d[strlen($d)-1] != '/')
$d .= '/';

$dh=@opendir($d);
if(!$dh)
return 0;

while($e = readdir($dh))
if($e != '.' && $e != '..')
$usage += disk_usage($d.$e, isset($depth) ? $depth – 1 : NULL);

closedir($dh);

return $usage;
}

/*
* format_filesize choses the best human readable unit for $bytes returns a formatted string
* Updates, documentation and examples: http://daniel.lorch.cc/projects/format_filesize/
*
* Revision: 1.00
*/

function format_filesize($bytes, $decimals = 1)
{
$units = array(
'1152921504606846976' => 'EB', /* Exa Byte 10^18 */
'1125899906842624' => 'PB', /* Peta Byte 10^15 */
'1099511627776' => 'TB',
'1073741824' => 'GB',
'1048576' => 'MB',
'1024' => 'KB'
);

if($bytes <= 1024)
return $bytes . " Bytes";

foreach($units as $base => $title)
if(floor($bytes / $base) != 0)
return number_format($bytes / $base, $decimals, ".", "'") . ' ' . $title;
}

?>

<?php
echo "Total Usage : ";
$usage3 = disk_usage('.');
$usage2 = format_filesize($usage3);
echo $usage2;
echo" (";
echo disk_usage('.');
echo")";

echo"<br>";
echo"<br>";

echo "Current Directory : ";
$usage3 = disk_usage('.', 0);
$usage2 = format_filesize($usage3);
echo $usage2;
echo" (";
echo disk_usage('.', 0);
echo")";
?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: