note 54712 deleted from function.filesize by aidan
| From: | aidan@php.net | Date: | Wed, 09 Nov 2005 06:14:12 +0000 |
| Subject: | note 54712 deleted from function.filesize by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-98291@lists.php.net to get a copy of this message | ||
Note Submitter: d [AT] sprid [DOT]de
----
Here my more future-save and much smaller function to convert bytes into a readable size:
<?php
/**
* function filesize_format() - version 1.0
* Formate a filesize to a human readable format
*
* @param integer bytes
* @param string the decimal delimiter, default '.'
* @param string the spacer between the filesize and the unit, default ' '
* @param boolean if set to true, all units are written in lowercase, default false
*
* @return string the formated filesize
*
* @access public
*
* @author DASPRiD <d@sprid.de>
*/
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) {
$broken_number = explode('.', $number);
if (!$broken_number[1])
return number_format(!$qty ? $bytes: $bytes /= $qty, 0, $decimal).$spacer.$unit;
else
return number_format(!$qty ? $bytes: $bytes /= $qty, 0,
$decimal).$decimal.substr($broken_number[1], 0, 2).$spacer.$unit;
}
}
}
} // end of 'filesize_format' function();
?>