downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

disk_total_space> <dirname
[edit] Last updated: Fri, 17 May 2013

view this page in

disk_free_space

(PHP 4 >= 4.1.0, PHP 5)

disk_free_spaceファイルシステムあるいはディスクパーティション上で利用可能な領域を返す

説明

float disk_free_space ( string $directory )

ディレクトリを指定することにより、 この関数は対応するファイルシステムまたはディスクパーティションで 利用可能なバイト数を返します。

パラメータ

directory

ファイルシステムのディレクトリあるいはディスクパーティション。

注意:

ディレクトリのかわりにファイル名を指定したときの挙動は未定義です。 OS や PHP のバージョンによって結果は異なります。

返り値

利用可能なバイト数を float 型で返します。 失敗した場合に FALSE を返します。

例1 disk_free_space() の例

<?php
// $df は「/」で利用可能なバイト数となります。
$df disk_free_space("/");

// Windows の場合:
$df_c disk_free_space("C:");
$df_d disk_free_space("D:");
?>

注意

注意: この関数では、 リモートファイル を 使用することはできません。これは、処理されるファイルがサーバーの ファイルシステムによりアクセスできる必要があるためです。

参考

  • disk_total_space() - ファイルシステムあるいはディスクパーティションの全体サイズを返す



disk_total_space> <dirname
[edit] Last updated: Fri, 17 May 2013
 
add a note add a note User Contributed Notes disk_free_space - [9 notes]
up
1
sam
4 years ago
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
up
1
rostvertol dot mil at gmail dot com
5 years ago
A cleaner and more efficient way of making human readable file sizes:

<?php
function decodeSize( $bytes )
{
   
$types = array( 'B', 'KB', 'MB', 'GB', 'TB' );
    for(
$i = 0; $bytes >= 1024 && $i < ( count( $types ) -1 ); $bytes /= 1024, $i++ );
    return(
round( $bytes, 2 ) . " " . $types[$i] );
}
?>
up
1
root at mantoru dot de
5 years ago
Note that disk_free_space() does an open_basedir check.
up
2
Nitrogen
6 years ago
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.
up
2
wiede at gmx dot net
2 years ago
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 />';
?>
up
0
djneoform at gmail dot com
6 years ago
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;
    }
?>
up
0
crrodriguez at opensuse dot org
1 year ago
Note that you should not rely on this function on linux BTRFS filesystems.read the FAQ for more info

https://btrfs.wiki.kernel.org/articles/f/a/q/FAQ_1fe9.htm
up
-1
Ashraf M Kaabi
8 years ago
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');
?>
up
-2
mixar at yandex dot ru
6 years ago
This the right function is:

function formatSize($size){
    switch (true){
    case ($size > 1099511627776):
        $size /= 1099511627776;
        $suffix = 'TB';
    break;
    case ($size > 1073741824):
        $size /= 1073741824;
        $suffix = 'GB';
    break;
    case ($size > 1048576):
        $size /= 1048576;
        $suffix = 'MB';   
    break;
    case ($size > 1024):
        $size /= 1024;
        $suffix = 'KB';
        break;
    default:
        $suffix = 'B';
    }
    return round($size, 2).$suffix;
}

 
show source | credits | sitemap | contact | advertising | mirror sites