disk_free_space() 和 disk_total_space() 在 PHP 中的用法

Usage of disk_free_space() and disk_total_space() in PHP

我不清楚 disk_free_space and disk_total_space 应该做什么。

文档分别表示 "Returns available space on filesystem or disk partition" 和 "Returns the total size of a filesystem or disk partition"。同时,在每种情况下,参数都被描述为 "A directory of the filesystem or disk partition."

那么我是否可以只为分区或特定文件夹获取可用磁盘space?

换句话说,这个脚本的两部分 return 相同的值是否正常?

<?php 

$path = '/home/bilingueanglais'; // an estimated 14 GB according to Webmin
$free = floor( disk_free_space( $path ) / ( 1024 * 1024 ) );
$total = floor( disk_total_space( $path ) / (1024 * 1024 ) );
echo 'Diskspace for path `' . $path . '`:<br>';
echo $free . 'MB out of ' . $total . ' MB<br><br><br>';

$path = '/home/bilingueanglais/public_html/assets/theme'; // just 225 KB in reality
$free = floor( disk_free_space( $path ) / ( 1024 * 1024 ) );
$total = floor( disk_total_space( $path ) / (1024 * 1024 ) );
echo 'Diskspace for path `' . $path . '`:<br>';
echo $free . 'MB out of ' . $total . ' MB<br><br><br>';

?>

它返回相同的值,因为这些函数分别专门查看 volume free/total 容量。

把它想象成 df 在 Linux/OS X/etc 上的输出 - 它显示 disk/volume 大小而不是目录大小。

这是*nix 约定。假设您在 /var/www:

上安装了一个文件系统

disk_free_space('/var/www'); 将 return 与 disk_free_space('/var/www/foobar.com'); 相同的结果,因为它们是相同的文件系统,这就是函数报告的内容。

您 运行 可能报告整个服务器的磁盘使用情况,而不仅仅是您的用户。