disk_free_space()

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

返回目录中的可用空间

说明disk_free_space(string$directory):float

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

参数$directory文件系统目录或者磁盘分区。Note:

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

返回值

以浮点返回可用的字节数,或者在失败时返回FALSE。

范例

Example #1disk_free_space()例子<?php

// $df 包含根目录下可用的字节数

$df = disk_free_space("/");

//在 Windows 下:

$df_c = disk_free_space("C:");

$df_d = disk_free_space("D:");

?>

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

参见Transformation is possible WITHOUT using loops:

$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 . '
';

echo sprintf('%1.2f', $bytes / pow($base,$class)) . ' '. $si_prefix[$class] . '
';

?>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$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'Another easy way to convert bytes to human readable sizes would be this:

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.Note that disk_free_space() does an open_basedir check.With respect to Linux filesystems, I'll point out that this function returns the space available in the current volume or mountpoint, not the total physical disk space. That is, this function used on the '/root' volume shows the free space in /root, which is different from '/home', and so on.<?php

function size($size, array $options=null) {

$o = [

'binary'=> false,

'decimalPlaces'=> 2,

'decimalSeparator'=> '.',

'thausandsSeparator'=> '',

'maxThreshold'=> false, // or thresholds key

'sufix'=> [

'thresholds'=> ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'],

'decimal'=> ' {threshold}B',

'binary'=> ' {threshold}iB'

]

];

if ($options !== null)

$o = array_replace_recursive($o, $options);

$count = count($o['sufix']['thresholds']);

$pow = $o['binary'] ? 1024 : 1000;

for ($i = 0; $i < $count; $i++)

if (($size < pow($pow, $i + 1)) ||

($i === $o['maxThreshold']) ||

($i === ($count - 1))

)

return

number_format(

$size / pow($pow, $i),

$o['decimalPlaces'],

$o['decimalSeparator'],

$o['thausandsSeparator']

) .

str_replace(

'{threshold}',

$o['sufix']['thresholds'][$i],

$o['sufix'][$o['binary'] ? 'binary': 'decimal']

);

}

var_dump(size(disk_free_space('/')));

// string(8) "14.63 GB"

var_dump(size(disk_free_space('/'), ['binary'=> true]));

// string(9) "13.63 GiB"

var_dump(size(disk_free_space('/'), ['maxThreshold'=> 2]));

// string(11) "14631.90 MB"

var_dump(size(disk_free_space('/'), ['binary'=> true, 'maxThreshold'=> 2]));

// string(12) "13954.07 MiB"

?>On Windows, this also works with distant files, by using their full network path.

For instance, this will give the % of free disk space on the share "dir" from remote host "server" :

$path = "\\\\server\\dir";

echo(floor(100 * disk_free_space($disk) / disk_total_space($disk)));

?>

It can also work with drive letters mapped to a network path in certain cases.

space index.php 7-14,disk_free_space()相关推荐

  1. space index.php 7-14,SpacePack高效部署PHP生产环境

    SpacePack 基于 Docker 为了快速部署 PHP 生产环境而产生的项目,它包含了一般项目中常用的组件,能够在最短的时间内产生一个完善并且优化过的 PHP 生产环境. 容器版本 SpaceP ...

  2. ddtv.space index.php,图解MongoDB原理(二)

    根据上篇文章<图解MongoDB原理>的基本介绍,本文以具体的操作为例继续深入解析mongodb. 演示的所有服务都是在一台机器上,我的目录是F:\JAVA\MongoDB\cluster ...

  3. 95.91p30.space\/index.php,关于 ThinkPHP6 分页样式的定制及点击下一页搜索条件丢失的解决方法...

    首先说一下前提条件是多应用模式下,假设每页显示 3 条记录. 控制器文件所在路径: /app/index/controller/DemoController.php 模板视图文件所在路径: /app/ ...

  4. 95.91p30.space\/index.php,快乐每一天

    不需要在网上找什么乱七八糟的东西,直接来之即用岂不是很完美. 只需要有一台服务器即可,没备案都可以玩这个功能.不需要拥有服务号,看完全文你就明白了. 数据库篇:-- Adminer 4.6.3 MyS ...

  5. Alter index coalesce VS shrink space

    10g中引入了对索引的shrink功能,索引shrink操作会扫描索引的页块,并且通过归并当前存在的数据将先前已删除记录的空间重新利用:很多书籍亦或者MOS的Note中都会提及SHRINK命令与早期版 ...

  6. Gerrit version 2.14.20 is now available

    Gerrit version 2.14.20 is now available 今天发现2.14版本的又有个个更新了.现在2.14更新到2.14.17版本了. gerrit.war 历史版本下载 各个 ...

  7. Pandas Index 属性

    import pandas as pd df = pd.DataFrame({"学号": [1001,1002,1003,1004,1005],"name": ...

  8. Ubuntu 14.04(64位)安装和使用docker

                          Docker介绍: Docker是一个开源的应用容器引擎,可以通过docker来安装一个独立的系统(类似于虚拟机(Vmware)之类的),不过其特点是非常轻 ...

  9. 极客日报:阿里旗下App接入微信支付;马斯克成世界首富;PostgreSQL 14 RC 1发布

    一分钟速览新闻点! 阿里回应App接入微信支付 抖音起诉知乎名誉侵权 小米上诉"小米穿戴"图形商标被驳回 拼多多.美团已支持众多主流支付渠道 清华AI学生华智冰首次露正脸唱歌 快手 ...

最新文章

  1. 入门 | CNN也能用于NLP任务,一文简述文本分类任务的7个模型
  2. linux使用同一密钥对实现互相免密登录
  3. linux命令行ps1变量_利用Shell中变量PS1定制Linux Shell命令主提示符
  4. 计数排序、桶排序和基数排序
  5. 数十亿次数学运算只消耗几毫瓦电力,谷歌开源Pixel 4背后的视觉模型
  6. 每周到岗上班3天,2天可在家办公!携程3+2工作模式来了
  7. Unity.Interception(AOP)
  8. xp连接win10工作组计算机,教你XP系统下连接win10共享的打印机的方法教程
  9. 不读后悔:风口上的仓储自动化
  10. 华为交换机释放vlanif接口DHCP地址池下ip命令
  11. 开年工作重点:帮助同事找到工作的价值
  12. 读书《AB实验:科学归因与增长的利器》(刘玉凤)
  13. 运动会分数统计的实验报告(数组实现)
  14. 【计算题】(六)微分方程和无穷级数
  15. 假如你想成为全栈工程师…
  16. KubeCon 2021中国大会
  17. 【电路】电容(一)——浅析大小电容的高低频滤波、并联问题
  18. Spring AOP中的静态代理和动态代理的原理和实践
  19. 学习日记day09 ps
  20. Java中的插入排序和希尔排序

热门文章

  1. telnet命令发送邮件
  2. WebLogic常见问题
  3. Laravel Composer 命令大全
  4. 【DICOMDIR专题】DICOMDIR基础知识及常见问题汇总
  5. 好久没有用c++,转一个c++注意点
  6. python web应用_为您的应用选择最佳的Python Web爬网库
  7. leetcode 402. 移掉K位数字(贪心算法)
  8. leetcode94. 二叉树的中序遍历(dfs)
  9. leetcode841. 钥匙和房间(bfs)
  10. leetcode852. 山脉数组的峰顶索引(二分法)