note 90272 deleted from function.filesize by danbrown
| From: | danbrown@php.net | Date: | Tue, 14 Apr 2009 03:34:21 +0000 |
| Subject: | note 90272 deleted from function.filesize by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-153196@lists.php.net to get a copy of this message | ||
Note Submitter: walter
----
Modified but slightly slower (who cares?) version of digitaldoener's function.
("35.2MB" vs. "35,20 MB")
- modified for non-european formatting
- removes useless trailing 0s on decimals, as well as the decimal point if the decimal is not
required (ie: ".00" number_format() output)
- just pass in the file name, function does the rest
<?
# returns file size in human readable form
function filesize_human($file) {
if(!file_exists($file)) { return ''; }
$bytes = filesize($file);
$b = (int)$bytes;
$s = array('B', 'KB', 'MB', 'GB', 'TB');
if($b < 0){
return "0 ".$s[0];
}
$con = 1024;
$e = (int)(log($b,$con));
return preg_replace('/(\.?0*?)?
/','',number_format($b/pow($con,$e),2,'.',',').'
'.$s[$e]);
}
?>
European format version (not tested, should work OK):
<?
# returns file size in human readable form (european formatting)
function filesize_human_european($file) {
if(!file_exists($file)) { return ''; }
$bytes = filesize($file);
$b = (int)$bytes;
$s = array('B', 'KB', 'MB', 'GB', 'TB');
if($b < 0){
return "0 ".$s[0];
}
$con = 1024;
$e = (int)(log($b,$con));
return preg_replace('/(,?0*?)?
/','',number_format($b/pow($con,$e),2,',','.').'
'.$s[$e]);
}
?>