note 55019 added to function.filesize
| From: | php-general at lists dot php dot net | Date: | Fri, 22 Jul 2005 15:35:42 +0000 |
| Subject: | note 55019 added to function.filesize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-92302@lists.php.net to get a copy of this message | ||
An improve of DASPRiD's code from the function to convert bytes into a readable size:
<?
function filesize_format ($bytes, $decimal='.', $spacer=' ', $lowercase=false) {
$bytes = max(0, (int)$bytes);
$units = array('YB' => 1208925819614629174706176, // yottabyte
'ZB' => 1180591620717411303424, // zettabyte
'EB' => 1152921504606846976, // exabyte
'PB' => 1125899906842624, // petabyte
'TB' => 1099511627776, // terabyte
'GB' => 1073741824, // gigabyte
'MB' => 1048576, // megabyte
'KB' => 1024, // kilobyte
'B' => 0); // byte
foreach ($units as $unit => $qty) {
if ($bytes >= $qty)
return number_format(!$qty ? $bytes: $bytes /= $qty, 2, $decimal, $spacer).$spacer.$unit;
}
} // end of 'filesize_format' function();
?>
For example:
INPUT: 1169
OUTPUT: 1.14 KB
----
Manual Page -- http://www.php.net/manual/en/function.filesize.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55019
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55019&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55019&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55019&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55019&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55019&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55019&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55019&report=yes
Search -- http://master.php.net/manage/user-notes.php