disk_free_space

(PHP 4 >= 4.1.0, PHP 5)

disk_free_space -- 返回目录中的可用空间

说明

float disk_free_space ( string directory )

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

例子 1. disk_free_space() 例子

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

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

参见 disk_total_space()


add a note add a note User Contributed Notes
djneoform at gmail dot com
12-Jul-2006 10: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;
   }
?>
flobee
12-May-2006 03:05
you may check this out working with % of disk usage (log object may comes from pear or just remove the log lines)
, flobee
<?php
// return true on error!
// on false: fine :-)
function check_disk_free_space($path) {
   global
$oLog;
  
// seconds a time-compare will be OK during the process (if it takes longer)
   // (limit the number if you have huge file movements to beware crashes
   // or "disk-full"- errors)
  
$secCmp = 60;
  
// max allowed/free size compare Value
  
$sizeCmp = 92;
  
// windows user get www.cygwin.com
  
$cmd = 'c:/cygwin/bin/df.exe -a '. $path;
  
$now = time();
  
$_v = array();
  
   static
$paths = null;
   static
$sizes = null;
   static
$times = null;
   if (
$paths===null) {
      
$paths = array();
      
$sizes = array();
      
$times = array();
   }

  
   if ((
$i=array_search($path,$paths)) && (($now-$times[$i]) < $secCmp)) {
       if(
$sizes[$i] >= $sizeCmp) {
          
$oLog->log('disc space overflow: '.$sizes[$i].' ('.$sizeCmp.' is max limit!) for path: '.$path, 3);
           return
false;
       } else {
          
$oLog->log('disc space OK: '.$sizes[$i].'% ('.$sizeCmp.' is max limit!) for path: '.$path, 6);
           return
true;
       }
   }

  
$oLog->log('getting free space for: '. $path, 6);

  
  
$result = exec($cmd, $data, $return);
  
$oLog->log('cmd: '. $cmd, 7);
  
   if (
$result) {
      
$r = explode(' ', $data[1]);
       while(list(
$a,$b) = each($r)) {
          
$b = trim($b);
           if (
$b != '') {
              
$_v[] = $b;
           }
       }
      
$oLog->log($_v, 7);
      
      
$size = intval($_v[4]);
      
      
$paths[] = $path;
      
$sizes[] = $size;
      
$times[] = time();
      
       if(
$size >= $sizeCmp) {
          
$oLog->log('disc space overflow: '.$sizes[$i].' ('.$sizeCmp.' is max limit!) for path: '.$path, 3);
           return
false;
       } else {
          
$oLog->log('disc space OK: '.$sizes[$i].' ('.$sizeCmp.' is max limit!) for path: '.$path, 6);
           return
true;
       }
      
      
/*
       echo '$return: ' . $return .'<br><pre>';
       echo  print_r($data);
       echo '</pre>';
       */
  
} else {
    
$oLog->log('error can not get size', 3);
     return
true;
   }
}

check_disk_free_space(getcwd());
?>
chicaboo at punkass dot com
09-May-2006 09:04
Just to let you know, 1 kilobyte = 1024 bytes, so from that perspective the code is alright
Martin Lindhe
25-Feb-2006 12:42
blow, your code is broken. it returns 1 kilobyte as "1024 bytes"

<?
function formatDataSize($bytes) {
  
$units = array('bytes', 'KiB', 'MiB', 'GiB', 'TiB');
   foreach (
$units as $unit) {
       if (
$bytes < 1024) break;
      
$bytes = round($bytes/1024, 1);
   }
   return
$bytes.' '.$unit;
}
?>
blow
04-Jan-2006 07:46
quiet the same

<?
function readable_size($lenght) {
  
$units = array('B', 'kB', 'MB', 'GB', 'TB');
   foreach (
$units as $unit) {
       if(
$lenght>1024) $lenght = round($lenght/1024, 1);
       else break;
   }
   return
$lenght.' '.$unit;
}
?>
Nick H
22-Nov-2005 12:08
This is probably what the previous poster indended to write:

<?php
function readable_size($size) {
   if (
$size < 1024) {
       return
$size . ' B';
   }
  
$units = array("kB", "MiB", "GB", "TB");
   foreach (
$units as $unit) {
      
$size = $size / 1024;
       if (
$size < 1024) {
           break;
       }
   }
   return
$size . ' ' . $unit;
}
?>
ludvig dot ericson at gmail dot com
27-Sep-2005 03:28
On the first note by aidan: it does not work.

On the second note, this is better:
<?
function readable_size($size) {
   if (
$size < 1024) {
       return
$size . ' B';
   }
  
$units = array("kB", "MiB", "GB", "TB");
   foreach (
$units as $unit) {
      
$size = size / 1024;
       if (
$size / 1024 > 1024) {
           break;
   }
   return
$size . ' ' . $unit;
}
?>
mat
20-Aug-2005 06:36
Note that in the previous two examples by Ashraf M Kaabi, calling the function in such a manner as disk_used_space(C) is in error and will give you a warning.  Should be disk_used_space('C'), otherwise it will look for a constant C, fail, and then act as if you had wrote 'C' instead.
Ashraf M Kaabi
02-Mar-2005 12: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);
?>
Ashraf M Kaabi
02-Mar-2005 01:55
and also you can get Human Disk Free Space result in GB in This Example :
<?
function dfs_gb($drive)
{
       if(
$drive)
       {
       return
round(diskfreespace("$drive:")/1024/1024/1024,2); //To Get Human Result in GB we divid it By 1024 Byte & 1024 KiloByte & 1024 MegaByte
      
}
}

echo
dfs_gb(D); //(D) is a drive letter
?>
aidan at php dot net
16-Oct-2004 08:49
To make human readable file sizes, see this function:

http://aidan.dotgeek.org/lib/?file=function.size_readable.php