PHP 5.4.31 Released

disk_free_space

(PHP 4 >= 4.1.0, PHP 5)

disk_free_spaceRestituisce lo spazio disponibile nella directory

Descrizione

float disk_free_space ( string $directory )

Data una stringa contenente una directory, questa funzione restituirà il numero di byte disponibili nel corrispondente filesystem o nella partizione corrispondente.

Example #1 Esempio disk_free_space()

<?php
// $df contiene il numero di byte disponibili in "/"
$df disk_free_space("/"); 

// Con Windows:
disk_free_space("C:");
disk_free_space("D:");
?>

Nota: Questa funzione non opererà su file remoti perché il file che deve essere esaminato deve essere accessibile attraverso il filesysmte del server.

Vedere anche disk_total_space()

add a note add a note

User Contributed Notes 5 notes

up
15
wiede at gmx dot net
3 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
4
sam
5 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
2
root at mantoru dot de
6 years ago
Note that disk_free_space() does an open_basedir check.
up
2
Nitrogen
7 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
1
Anonymous
4 days 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'
To Top