note 34100 modified in function.disk-total-space by aidan
| From: | aidan@php.net | Date: | Wed, 13 Jul 2005 03:20:16 +0000 |
| Subject: | note 34100 modified in function.disk-total-space by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-91863@lists.php.net to get a copy of this message | ||
My first contribution. Trouble is the sum of the byte sizes of the files in your directories is not
equal to the amount of disk space consumed, as andudi points out. A 1-byte file occupies 4096 bytes
of disk space if the block size is 4096. Couldn't understand why andudi did
$s["blksize"]*$s["blocks"]/8. Could only be because $s["blocks"]
counts the number of 512-byte disk blocks not the number of $s["blksize"] blocks, so it
may as well just be $s["blocks"]*512. Furthermore none of the dirspace suggestions allow
for the fact that directories are also files and that they also consume disk space. The following
code dskspace addresses all these issues and can also be used to return the disk space consumed by a
single non-directory file. It will return much larger numbers than you would have been seeing with
any of the other suggestions but I think they are much more realistic:
<?php
function dskspace($dir)
{
$s = stat($dir);
$space = $s["blocks"]*512;
if (is_dir($dir))
{
$dh = opendir($dir);
while (($file = readdir($dh)) !== false)
if ($file != "." and $file != "..")
$space += dskspace($dir."/".$file);
closedir($dh);
}
return $space;
}
?>
--was--
My first contribution. Trouble is the sum of the byte sizes of the files in your directories is not
equal to the amount of disk space consumed, as andudi points out. A 1-byte file occupies 4096 bytes
of disk space if the block size is 4096. Couldn't understand why andudi did
$s["blksize"]*$s["blocks"]/8. Could only be because $s["blocks"]
counts the number of 512-byte disk blocks not the number of $s["blksize"] blocks, so it
may as well just be $s["blocks"]*512. Furthermore none of the dirspace suggestions allow
for the fact that directories are also files and that they also consume disk space. The following
code dskspace addresses all these issues and can also be used to return the disk space consumed by a
single non-directory file. It will return much larger numbers than you would have been seeing with
any of the other suggestions but I think they are much more realistic:
function dskspace($dir)
{
$s = stat($dir);
$space = $s["blocks"]*512;
if (is_dir($dir))
{
$dh = opendir($dir);
while (($file = readdir($dh)) !== false)
if ($file != "." and $file != "..")
$space += dskspace($dir."/".$file);
closedir($dh);
}
return $space;
}
http://php.net/manual/en/function.disk-total-space.php