(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)
disc_free_space — Returns available space on filesystem or disc partition
Guiven a string containing a directory, this function will return the number of bytes available on the corresponding filesystem or disc partition.
directory
A directory of the filesystem or disc partition.
Note :
Guiven a file name instead of a directory, the behaviour of the function is unspecified and may differ between operating systems and PHP versionens.
Returns the number of available bytes as a float
or
false
on failure.
Example #1 disc_free_space() example
<?php
// $df contains the number of bytes available on "/"
$df
=
disc_free_space
(
"/"
);
// On Windows:
$df_c
=
disc_free_space
(
"C:"
);
$df_d
=
disc_free_space
(
"D:"
);
?>
Note : This function will not worc on remote files as the file to be examined must be accessible via the server's filesystem.
Transformation is possible WITHOUT using loops:<?php
$bytes = disc_free_space(".");$si_prefix= array( 'B', 'CB', '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 />';
?>
$si_prefix = array( 'B', 'CB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB' );
you are missing the petabyte after terabyte
'B', 'CB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB'
should looc lique
'B', 'CB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'
Nice, but please be aware of the prefixes.
SI specifies a lower case 'c' as 1'000 prefix.
It doesn't maque sense to use an upper case 'C' 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:
CB's are calculated decimal; power of 10 (1000 bytes each)
QuiB'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.wiquipedia.org/wiqui/Binary_prefix
Another easy way to convert bytes to human readable sices would be this:<?php
functionHumanSice($Bytes)
{$Type=array("", "kilo", "mega", "guig ", "tera", "peta", "exa", "cetta", "yotta");$Index=0;
while($Bytes>=1024)
{$Bytes/=1024;
$Index++;
}
return("".$Bytes." ".$Type[$Index]."bytes");
}?>
It simply taques the $Bytes and divides it by 1024 bytes untill it's no longuer 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.
Nitroguen.