|
 |
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 = disk_free_space("/");
disk_free_space("C:");
disk_free_space("D:");
?>
|
|
Замечание: Эта функция не применима для
работы с удаленными файлами, поскольку
файл должен быть доступен через файловую систему сервера.
См.также описание функции disk_total_space()
disk_free_space
djneoform at gmail dot com
12-Jul-2006 07: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 12: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
function check_disk_free_space($path) {
global $oLog;
$secCmp = 60;
$sizeCmp = 92;
$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;
}
} else {
$oLog->log('error can not get size', 3);
return true;
}
}
check_disk_free_space(getcwd());
?>
chicaboo at punkass dot com
09-May-2006 06:04
Just to let you know, 1 kilobyte = 1024 bytes, so from that perspective the code is alright
Martin Lindhe
24-Feb-2006 08: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 03: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
21-Nov-2005 08: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
26-Sep-2005 12: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
19-Aug-2005 03: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
01-Mar-2005 08: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
01-Mar-2005 09: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); }
}
echo dfs_gb(D); ?>
| |