PHP 7.0.6 Released

disk_free_space

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

disk_free_spaceReturns available space on filesystem or disk partition

Description

float disk_free_space ( string $directory )

Given a string containing a directory, this function will return the number of bytes available on the corresponding filesystem or disk partition.

Parameters

directory

A directory of the filesystem or disk partition.

Note:

Given a file name instead of a directory, the behaviour of the function is unspecified and may differ between operating systems and PHP versions.

Return Values

Returns the number of available bytes as a float or FALSE on failure.

Examples

Example #1 disk_free_space() example

<?php
// $df contains the number of bytes available on "/"
$df disk_free_space("/");

// On Windows:
$df_c disk_free_space("C:");
$df_d disk_free_space("D:");
?>

Notes

Note: This function will not work on remote files as the file to be examined must be accessible via the server's filesystem.

See Also

User Contributed Notes

wiede at gmx dot net
5 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 />';
?>
Anonymous
1 year ago
$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'
root at mantoru dot de
8 years ago
Note that disk_free_space() does an open_basedir check.
sam
7 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
Nitrogen
9 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.
polakeng at gmail dot com
1 year ago
If you want to show the free kbs / total kbs use this code:

<?
$dir = 'home/'
$free = disk_free_space($dir);
$total = disk_total_space($dir);
$free_to_kbs = $free / (1024*1024);
$total_to_kbs = $total / (1024*1024);
echo 'You have '.$free_to_kbs.' KBs from '.$total_to_kbs.' total KBs';
?>

and for MBs:

<?
$dir = 'home/'
$free = disk_free_space($dir);
$total = disk_total_space($dir);
$free_to_mbs = $free / (1024*1024)*1024;
$total_to_mbs = $total / (1024*1024)*1024;
echo 'You have '.$free_to_mbs.' MBs from '.$total_to_mbs.' total MBs';
?>

Neymar11
To Top