php disk_total_space和disk_free_space函数实例

时间:2016-08-08
php disk_total_space函数获取一个目录的磁盘总大小,disk_free_space函数获取目录中的可用空间 ,本文章向大家介绍disk_total_space和disk_free_space的使用方法和实例,需要的朋友可以参考一下。

disk_total_space函数介绍

disk_total_space — 返回一个目录的磁盘总大小,以浮点返回一个目录的磁盘总大小字节数, 或者在失败时返回 FALSE

语法:

float disk_total_space  ( string $directory  )

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

参数:

  1. directory:文件系统的目录或者磁盘分区。

disk_free_space函数介绍

disk_free_space — 返回目录中的可用空间,以浮点返回可用的字节数, 或者在失败时返回 FALSE 。

语法:

float disk_free_space  ( string $directory  )

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

参数:

  1. directory 文件系统目录或者磁盘分区。如果指定了文件名而不是文件目录,这个函数的行为将并不统一,会因操作系统和 PHP 版本而异。

disk_free_space和disk_total_space实例

1、Get free disk space for C: drive

<?
$space = diskfreespace("C:\\");
echo "$space";
?>  

2、Get disk free space for Unix/Linux

<?
$space = diskfreespace("/");
echo "$space";
?>

3、Disk total space

<?php
   $systempartitions = array("/", "/home","/usr", "/www");
   foreach ($systempartitions as $partition) {
      $totalSpace = disk_total_space($partition) / 1048576;
      /* http://www.manongjc.com/article/1324.html */
      $usedSpace = $totalSpace - disk_free_space($partition) / 1048576;
      echo "Partition: $partition (Allocated: $totalSpace MB. Used: $usedSpace MB.) <BR>";
   }
?>