note 25922 deleted from function.disk-total-space by cece
| From: | cece@php.net | Date: | Tue, 19 Aug 2003 12:45:32 +0000 |
| Subject: | note 25922 deleted from function.disk-total-space by cece | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-54667@lists.php.net to get a copy of this message | ||
Note Submitter: pdontthink at angrynerds NO*SPAM dot com
----
This is similar to the dirsize functions above except it adds:
- Take into account directories that you don't have permission to open (nasty results
otherwise)
- Catch errors and return -1 (remember to check for this!)
- Don't follow symlinks (I'm implementing a quota system, so users shouldn't be
responsible for contents of symlinked directories):
You can call it like this:
$size = dirsize('some/dir/path');
if ($size < 0) echo 'ERROR! Bad path!';
else echo 'Your dirsize is ' . format_size($size);
And the relevant functions are:
// returns the total size, in bytes, of all files
// contained in the directory tree beginning at the
// specified directory
// returns -1 if any problem occured (such as if the
// directory given was nonexistent)
//
function dirsize($directory)
{
if (!is_dir($directory)) return -1;
$size = 0;
if ($DIR = opendir($directory))
{
while (($dirfile = readdir($DIR)) !== false)
{
if (is_link($directory . '/' . $dirfile) || $dirfile == '.' ||
$dirfile == '..')
continue;
if (is_file($directory . '/' . $dirfile))
$size += filesize($directory . '/' . $dirfile);
else if (is_dir($directory . '/' . $dirfile))
{
$dirSize = dirsize($directory . '/' . $dirfile);
if ($dirSize >= 0) $size += $dirSize;
else return -1;
}
}
closedir($DIR);
}
return $size;
}
// stolen from another fine user on php.net
//
function format_size($rawSize)
{
if ($rawSize / 1048576 > 1)
return round($rawSize/1048576, 1) . 'MB';
else if ($rawSize / 1024 > 1)
return round($rawSize/1024, 1) . 'KB';
else
return round($rawSize, 1) . 'bytes';
}