disk_free_space

(PHP 4 >= 4.0.7, PHP 5)

disk_free_space — Получить размер доступного пространства в каталоге

Описание

float disk_free_space ( string $directory )

Функция возвращает размер свободного пространства в байтах, доступного для использования в указанном разделе диска.

Пример #1 Пример использования функции disk_free_space()

<?php
$df 
disk_free_space("/");
// $df содержит размер свободного места в каталоге "/"

// Под Windows:
disk_free_space("C:");
disk_free_space("D:");
?>

Замечание: Эта функция не применима для работы с удаленными файлами, поскольку файл должен быть доступен через файловую систему сервера.

См.также описание функции disk_total_space()

Коментарии

Автор:
Another easy way to convert bytes to human readable sizes would be this:

<?php
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.
2007-01-10 19:50:45
http://php5.kiev.ua/manual/ru/function.disk-free-space.html
Note that disk_free_space() does an open_basedir check.
2007-12-06 10:25:43
http://php5.kiev.ua/manual/ru/function.disk-free-space.html
Автор:
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
2008-12-17 15:52:16
http://php5.kiev.ua/manual/ru/function.disk-free-space.html
Transformation is possible WITHOUT using loops:

<?php 
    $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 '<br />';
    echo 
sprintf('%1.2f' $bytes pow($base,$class)) . ' ' $si_prefix[$class] . '<br />';
?>
2011-04-11 08:36:01
http://php5.kiev.ua/manual/ru/function.disk-free-space.html
Автор:
$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'
2014-07-21 12:14:05
http://php5.kiev.ua/manual/ru/function.disk-free-space.html
Автор:
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.
2016-10-23 12:04:19
http://php5.kiev.ua/manual/ru/function.disk-free-space.html
Автор:
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" :
<?php
$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.
2016-12-21 01:41:36
http://php5.kiev.ua/manual/ru/function.disk-free-space.html
<?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"

?>
2019-06-20 14:58:55
http://php5.kiev.ua/manual/ru/function.disk-free-space.html
Автор:
This is not documented yet. 
If $directory is invalid, then disk_free_space() will return false and ALSO throw a Warning: "disk_free_space(): No such file or directory"
2021-12-27 15:07:01
http://php5.kiev.ua/manual/ru/function.disk-free-space.html

    Поддержать сайт на родительском проекте КГБ