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 />';
?>disk_free_space
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
disk_free_space
Референца за `function.disk-free-space.php` со подобрена типографија и навигација.
disk_free_space
(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)
disk_free_space — Враќа достапен простор на файловиот систем или партицијата на дискот
= NULL
Даден стринг што содржи директориум, оваа функција ќе го врати бројот на достапни бајти на соодветниот файлов систем или партиција на дискот.
Параметри
directory-
Директориум на файловиот систем или партицијата на дискот.
Забелешка:
Дадено име на датотека наместо директориум, однесувањето на функцијата е неопределено и може да се разликува помеѓу оперативните системи и верзиите на PHP.
Вратени вредности
Враќа број на достапни бајти како float или false при неуспех.
Примери
Пример #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:");
?>Белешки
Забелешка: Оваа функција нема да работи на Оваа опција овозможува fopen обвивки свесни за URL-и кои овозможуваат пристап до URL објекти како датотеки. Стандардни обвивки се обезбедени за пристап до бидејќи датотеката што треба да се испита мора да биде достапна преку датотечниот систем на серверот.
Белешки од корисници 5 белешки
$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'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_prefixAnother 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.