©
Ce document utilise Manuel du site Web PHP chinois Libérer
(PHP 4 >= 4.1.0, PHP 5, PHP 7)
disk_total_space — 返回一个目录的磁盘总大小
$directory
)给出一个包含有一个目录的字符串,本函数将根据相应的文件系统或磁盘分区返回所有的字节数。 【译者注】本函数返回的是该目录所在的磁盘分区的总大小,因此在给出同一个磁盘分区的不同目录作为参数所得到的结果完全相同。 在 Unix 和 Windows 200x/XP 中都支持将一个磁盘分区加载为一个子目录,这时正确使用本函数就很有意义。
directory
文件系统的目录或者磁盘分区。
以浮点返回一个目录的磁盘总大小字节数, 或者在失败时返回 FALSE
。
Example #1 disk_total_space() 例子
<?php
// $df 包含 "/" 目录的磁盘大小
$ds = disk_total_space ( "/" );
//在 Windows 下:
$ds = disk_total_space ( "C:" );
$ds = disk_total_space ( "D:" );
?>
Note: 此函数不能作用于远程文件,被检查的文件必须是可通过服务器的文件系统访问的。
[#1] ryan at designedbyrayn dot co dot uk [2014-01-29 21:06:48]
<?php
//This is a more readable way of viewing the returned float
//?$Bytes?contains?the?total number?of?bytes on "/"
$Bytes?=?disk_total_space("/");
function?dataSize($Bytes)
{
$Type=array("",?"kilo",?"mega",?"giga",?"tera");
$counter=0;
while($Bytes>=1024)
{
$Bytes/=1024;
$counter++;
}
return("".$Bytes." ".$Type[$counter]."bytes");
}
?>
[#2] khumlo at gmail dot com [2010-05-23 01:36:49]
PHP 6 has already come to the market but still there are no standard function that can help retrieve directory size as it has to calculate disk space such as disk_total_space. Although we can use system level call such as exec() and system(), it is too risky to enable these function. So we look for an alternate method so as to calculate directory size.
Sol: retrieving directory size using php
<?php
function get_dir_size($dir_name){
$dir_size =0;
if (is_dir($dir_name)) {
if ($dh = opendir($dir_name)) {
while (($file = readdir($dh)) !== false) {
if($file !=??.?? && $file != ??..??){
if(is_file($dir_name.??/??.$file)){
$dir_size += filesize($dir_name.??/??.$file);
}
if(is_dir($dir_name.??/??.$file)){
$dir_size += get_dir_size($dir_name.??/??.$file);
}
}
}
}
}
closedir($dh);
return $dir_size;
}
$dir_name = ??directory name here??;
$total_size= round((get_dir_size($dir_name) / 1048576),2) ;
print ??Directory $dir_name size : $total_size MB??;
?>
Depending upon the size format you want to achieve, divide directory size by 1024 or 1024 * 1024 or 1024 * 1024 * 1024 for KB or MB or GB respectively.
[#3] cotact[at]covac-software[dot]com [2009-12-25 06:52:24]
Something that might go well with this function is the ability to list available disks. On Windows, here's the relevant code:
<?php
function get_disks(){
if(php_uname('s')=='Windows NT'){
// windows
$disks=`fsutil fsinfo drives`;
$disks=str_word_count($disks,1);
if($disks[0]!='Drives')return '';
unset($disks[0]);
foreach($disks as $key=>$disk)$disks[$key]=$disk.':\\';
return $disks;
}else{
// unix
$data=`mount`;
$data=explode(' ',$data);
$disks=array();
foreach($data as $token)if(substr($token,0,5)=='/dev/')$disks[]=$token;
return $disks;
}
}
?>
EXAMPLE OF USE:
<?php print_r(get_disks()); ?>
EXAMPLE RESULT:
Array
(
[1] => A:\
[2] => C:\
[3] => D:\
[4] => E:\
[5] => F:\
[6] => G:\
[7] => H:\
[8] => I:\
[9] => M:\
[10] => X:\
[11] => Z:\
)
Warning: This also finds empty disk drives (eg; CD or SMD drives or the more common floppy drive).
Warning2: If you want to find space usage using the info from my function, prepend the disk function with the "@", eg:
$free=@disk_free_space('A:\\');
[#4] Viitala [2008-02-04 01:04:10]
Beware of empty files!
<?php
// Wrong
$exp = floor(log($bytes) / log(1024));
//Correct
$exp = $bytes ? floor(log($bytes) / log(1024)) : 0;
?>
[#5] tularis at php dot net [2007-06-25 03:13:26]
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))));
}
[#6] stierguy1 at msn dot com [2007-06-24 19:03:53]
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]);}
[#7] nikolayku at tut dot by [2007-04-17 13:16:02]
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);
}
[#8] JulieC [2007-01-30 19:11:21]
"filesystem or disk partition" does not equal "directory" for Windows. Thanks.
[#9] andudi at gmx dot ch [2002-06-11 15:15:37]
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