PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

diskfreespace> <disk_free_space
Last updated: Mon, 26 Nov 2007

view this page in

disk_total_space

(PHP 4 >= 4.0.7, PHP 5)

disk_total_space — 返回一个目录的磁盘总大小

说明

float disk_total_space ( string $directory )

给出一个包含有一个目录的字符串,本函数将根据相应的文件系统或磁盘分区返回所有的字节数。

Example#1 disk_total_space() 例子

<?php
// $df 包含 "/" 目录的磁盘大小
$df disk_total_space("/");

//在 Windows 下:
disk_total_space("C:");
disk_total_space("D:");
?>

Note: 本函数不能作用于远程文件,被检查的文件必须通过服务器的文件系统访问。

参见 disk_free_space()

【译者注】本函数返回的是该目录所在的磁盘分区的总大小,因此在给出同一个磁盘分区的不同目录作为参数所得到的结果完全相同。在 Unix 和 Windows 200x/XP 中都支持将一个磁盘分区加载为一个子目录,这时正确使用本函数就很有意义。



diskfreespace> <disk_free_space
Last updated: Mon, 26 Nov 2007
 
add a note add a note User Contributed Notes
disk_total_space
Viitala
04-Feb-2008 05:04
Beware of empty files!

<?php

   
// Wrong
   
$exp = floor(log($bytes) / log(1024));

   
//Correct
   
$exp = $bytes ? floor(log($bytes) / log(1024)) : 0;

?>
tularis at php dot net
25-Jun-2007 06:13
For a non-looping way to add symbols to a number of bytes:
<?php
function getSymbolByQuantity($bytes) {
   
$symbols = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
   
$exp = floor(log($bytes)/log(1024));

    return
sprintf('%.2f '.$symbol[$exp], ($bytes/pow(1024, floor($exp))));
}
stierguy1 at msn dot com
25-Jun-2007 10:03
function roundsize($size){
    $i=0;
    $iec = array("B", "Kb", "Mb", "Gb", "Tb");
    while (($size/1024)>1) {
        $size=$size/1024;
        $i++;}
    return(round($size,1)." ".$iec[$i]);}
nikolayku at tut dot by
18-Apr-2007 04:16
Very simple function that convert bytes to kilobytes, megabytes ...

function ConvertBytes($number)
{
    $len = strlen($number);
    if($len < 4)
    {
        return sprintf("%d b", $number);
    }
    if($len >= 4 && $len <=6)
    {
        return sprintf("%0.2f Kb", $number/1024);
    }
    if($len >= 7 && $len <=9)
    {
        return sprintf("%0.2f Mb", $number/1024/1024);
    }
   
    return sprintf("%0.2f Gb", $number/1024/1024/1024);
                           
}
Mat
05-Apr-2007 06:46
JulieC:

I think you may have misunderstood - given a directory, this function tells you how big the disk paritition is that the directory exists on.

So disk_total_space("C:\Windows\") will tell you how big drive C is.

It is not suggesting that a directory is a disk partition.
JulieC
31-Jan-2007 11:11
"filesystem or disk partition" does not equal "directory" for Windows.  Thanks.
martijn at mo dot com
14-Jan-2007 06:41
This works for me (on a UNIX server):

<?php
function du( $dir )
{
   
$res = `du -sk $dir`;             // Unix command
   
preg_match( '/\d+/', $res, $KB ); // Parse result
   
$MB = round( $KB[0] / 1024, 1 );  // From kilobytes to megabytes
   
return $MB;
}

$dirSize = du('/path/to/dir/');
?>
shalless at rubix dot net dot au
16-Jul-2003 10:36
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;
}
?>
andudi at gmx dot ch
12-Jun-2002 06:15
To find the total size of a file/directory you have to differ two situations:
(on Linux/Unix based systems only!?)

you are interested:
1) in the total size of the files in the dir/subdirs
2) what place on the disk your dir/subdirs/files uses

- 1) and 2) normaly differs, depending on the size of the inodes
- mostly 2) is greater than 1) (in the order of any kB)
- filesize($file) gives 1)
- "du -ab $file" gives 2)

so you have to choose your situation!

on my server I have no rights to use "exec du" in the case of 2), so I use:
  $s = stat($file);
  $size = $s[11]*$s[12]/8);
whitch is counting the inodes [12] times the size of them in Bits [11]

hopes this helps to count the used disk place in a right way... :-)

                     Andreas Dick

diskfreespace> <disk_free_space
Last updated: Mon, 26 Nov 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites