PHP 8.4.0 RC4 available for testing

disk_free_space

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

disk_free_spaceDevuelve el espacio disponible de un sistema de archivos o partición de disco

Descripción

disk_free_space(string $directory): float

Dada una cadena que contiene un directorio, esta función devolverá el número de bytes disponibles en el sistema de archivos o partición de disco correspondiente.

Parámetros

directory

Un directorio del sistema de archivos o partición de disco.

Nota:

Dado un nombre de archivo en vez de un directorio, el comportamiento de la función no está especificado y puede diferir entre sistemas operativos y versiones de PHP.

Valores devueltos

Devuelve el número de bytes disponibles como un float o false en caso de error.

Ejemplos

Ejemplo #1 Ejemplo de disk_free_space()

<?php
// $df contiene el número de bytes disponibles en "/"
$df = disk_free_space("/");

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

Notas

Nota: Esta función no funcionará en ficheros remotos ya que el fichero debe ser accesible vía el sistema de ficheros del servidor para poder ser examinado.

Ver también

  • disk_total_space() - Devuelve el tamaño total de un sistema de archivos o partición de disco

add a note

User Contributed Notes 7 notes

up
66
wiede at gmx dot net
13 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
33
Anonymous
10 years 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'
up
17
sam
15 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
10
Nitrogen
17 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
8
root at mantoru dot de
16 years ago
Note that disk_free_space() does an open_basedir check.
up
0
Jawira Portugal
2 years ago
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"
up
-1
somedude
8 years ago
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.
To Top