note 90272 added to function.filesize
| From: | walter at osu1 dot php dot net | Date: | Tue, 14 Apr 2009 02:22:33 +0000 |
| Subject: | note 90272 added to function.filesize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-153194@lists.php.net to get a copy of this message | ||
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]);
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 72.9.105.34
----
Manual Page -- http://www.php.net/manual/en/function.filesize.php
Edit -- https://master.php.net/note/edit/90272
Del: integrated -- https://master.php.net/note/delete/90272/integrated
Del: useless -- https://master.php.net/note/delete/90272/useless
Del: bad code -- https://master.php.net/note/delete/90272/bad+code
Del: spam -- https://master.php.net/note/delete/90272/spam
Del: non-english -- https://master.php.net/note/delete/90272/non-english
Del: in docs -- https://master.php.net/note/delete/90272/in+docs
Del: other reasons-- https://master.php.net/note/delete/90272
Reject -- https://master.php.net/note/reject/90272
Search -- https://master.php.net/manage/user-notes.php