Filesystem 函数
在线手册:中文 英文
PHP手册

disk_free_space

(PHP 4 >= 4.1.0, PHP 5)

disk_free_space返回目录中的可用空间

说明

float disk_free_space ( string $directory )

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

Example #1 disk_free_space() 例子

<?php
// $df 包含根目录下可用的字节数
$df disk_free_space("/");
//在 Windows 下:
disk_free_space("C:");
disk_free_space("D:");
?>

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

参见 disk_total_space()

参数

directory

A directory of the filesystem or disk partition.

Note:

Given a file name instead of a directory, the behaviour of the function is unspecified and may differ between operating systems and PHP versions.

返回值

Returns the number of available bytes as a float 或者在失败时返回 FALSE.

范例

Example #2 disk_free_space() example

<?php
// $df contains the number of bytes available on "/"
$df disk_free_space("/");

// On Windows:
$df_c disk_free_space("C:");
$df_d disk_free_space("D:");
?>

注释

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

参见


Filesystem 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: 返回目录中的可用空间

用户评论:

crrodriguez at opensuse dot org (20-Dec-2011 09:54)

Note that you should not rely on this function on linux BTRFS filesystems.read the FAQ for more info

https://btrfs.wiki.kernel.org/articles/f/a/q/FAQ_1fe9.htm

wiede at gmx dot net (11-Apr-2011 01:36)

Transformation is possible WITHOUT using loops:

<?php
    $bytes
= disk_free_space(".");
   
$si_prefix = array( 'B', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB' );
   
$base = 1024;
   
$class = min((int)log($bytes , $base) , count($si_prefix) - 1);
    echo
$bytes . '<br />';
    echo
sprintf('%1.2f' , $bytes / pow($base,$class)) . ' ' . $si_prefix[$class] . '<br />';
?>

sam (17-Dec-2008 09:52)

Nice, but please be aware of the prefixes.

SI specifies a lower case 'k' as 1'000 prefix.
It doesn't make sense to use an upper case 'K' as binary prefix,
while the decimal Mega (M and following) prefixes in SI are uppercase.
Furthermore, there are REAL binary prefixes since a few years.

Do it the (newest and recommended) "IEC" way:

KB's are calculated decimal; power of 10 (1000 bytes each)
KiB's are calculated binary; power of 2 (1024 bytes each).
The same goes for MB, MiB and so on...

Feel free to read:
http://en.wikipedia.org/wiki/Binary_prefix

rostvertol dot mil at gmail dot com (18-Feb-2008 07:27)

A cleaner and more efficient way of making human readable file sizes:

<?php
function decodeSize( $bytes )
{
   
$types = array( 'B', 'KB', 'MB', 'GB', 'TB' );
    for(
$i = 0; $bytes >= 1024 && $i < ( count( $types ) -1 ); $bytes /= 1024, $i++ );
    return(
round( $bytes, 2 ) . " " . $types[$i] );
}
?>

root at mantoru dot de (06-Dec-2007 04:25)

Note that disk_free_space() does an open_basedir check.

Nitrogen (11-Jan-2007 01:50)

Another easy way to convert bytes to human readable sizes would be this:

<?php
function HumanSize($Bytes)
{
 
$Type=array("", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta");
 
$Index=0;
  while(
$Bytes>=1024)
  {
   
$Bytes/=1024;
   
$Index++;
  }
  return(
"".$Bytes." ".$Type[$Index]."bytes");
}
?>

It simply takes the $Bytes and divides it by 1024 bytes untill it's no longer over or equal to 1024, meanwhile it increases the $Index to allocate which suffix belongs to the return (adding 'bytes' to the end to save some space).
You can easily modify it so it's shorter, but I made it so it's more clearer.

Nitrogen.

mixar at yandex dot ru (04-Dec-2006 11:33)

This the right function is:

function formatSize($size){
    switch (true){
    case ($size > 1099511627776):
        $size /= 1099511627776;
        $suffix = 'TB';
    break;
    case ($size > 1073741824):
        $size /= 1073741824;
        $suffix = 'GB';
    break;
    case ($size > 1048576):
        $size /= 1048576;
        $suffix = 'MB';   
    break;
    case ($size > 1024):
        $size /= 1024;
        $suffix = 'KB';
        break;
    default:
        $suffix = 'B';
    }
    return round($size, 2).$suffix;
}

djneoform at gmail dot com (12-Jul-2006 03:13)

List all drives, free space, total space and percentage free.

<?
    for ($i = 67; $i <= 90; $i++)
    {
        $drive = chr($i);
        if (is_dir($drive.':'))
        {
            $freespace             = disk_free_space($drive.':');
            $total_space         = disk_total_space($drive.':');
            $percentage_free     = $freespace ? round($freespace / $total_space, 2) * 100 : 0;
            echo $drive.': '.to_readble_size($freespace).' / '.to_readble_size($total_space).' ['.$percentage_free.'%]<br />';
        }
    }

    function to_readble_size($size)
    {
        switch (true)
        {
            case ($size > 1000000000000):
                $size /= 1000000000000;
                $suffix = 'TB';
                break;
            case ($size > 1000000000):
                $size /= 1000000000;
                $suffix = 'GB';
                break;
            case ($size > 1000000):
                $size /= 1000000;
                $suffix = 'MB';   
                break;
            case ($size > 1000):
                $size /= 1000;
                $suffix = 'KB';
                break;
            default:
                $suffix = 'B';
        }
        return round($size, 2).$suffix;
    }
?>

Ashraf M Kaabi (02-Mar-2005 04:38)

and also you can know the used space , in this
example :
<?
function disk_used_space($drive)
{
    return disk_total_space("$drive:") - disk_free_space("$drive:");
}

echo disk_used_space('C');
?>