©
本文档使用 PHP中文网手册 发布
(PHP 4 >= 4.1.0, PHP 5, PHP 7)
disk_free_space — 返回目录中的可用空间
$directory
)给出一个包含有一个目录的字符串,本函数将根据相应的文件系统或磁盘分区返回可用的字节数。
directory
文件系统目录或者磁盘分区。
Note:
如果指定了文件名而不是文件目录,这个函数的行为将并不统一,会因操作系统和 PHP 版本而异。
以浮点返回可用的字节数, 或者在失败时返回 FALSE
。
Example #1 disk_free_space() 例子
<?php
// $df 包含根目录下可用的字节数
$df = disk_free_space ( "/" );
//在 Windows 下:
$df_c = disk_free_space ( "C:" );
$df_d = disk_free_space ( "D:" );
?>
Note: 此函数不能作用于远程文件,被检查的文件必须是可通过服务器的文件系统访问的。
[#1] polakeng at gmail dot com [2015-04-09 12:28:45]
If you want to show the free kbs / total kbs use this code:
<?php
$dir = 'home/'
$free = disk_free_space($dir);
$total = disk_total_space($dir);
$free_to_kbs = $free / (1024*1024);
$total_to_kbs = $total / (1024*1024);
echo 'You have '.$free_to_kbs.' KBs from '.$total_to_kbs.' total KBs';
?>
and for MBs:
<?php
$dir = 'home/'
$free = disk_free_space($dir);
$total = disk_total_space($dir);
$free_to_mbs = $free / (1024*1024)*1024;
$total_to_mbs = $total / (1024*1024)*1024;
echo 'You have '.$free_to_mbs.' MBs from '.$total_to_mbs.' total MBs';
?>
Neymar11
[#2] Anonymous [2014-07-21 09:14:05]
$si_prefix = array( 'B', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB' );
you are missing the petabyte after terabyte
'B', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB'
should look like
'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'
[#3] wiede at gmx dot net [2011-04-11 05:36:01]
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 />';
?>
[#4] sam [2008-12-17 13:52:16]
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
[#5] root at mantoru dot de [2007-12-06 08:25:43]
Note that disk_free_space() does an open_basedir check.
[#6] Nitrogen [2007-01-10 17:50:45]
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.