note 120250 added to function.filesize
| From: | evgenij at kostanay dot kz | Date: | Thu, 01 Dec 2016 09:05:28 +0000 |
| Subject: | note 120250 added to function.filesize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-207828@lists.php.net to get a copy of this message | ||
Slightly edited version of the function from rommel at rommelsantor dot com. Now it returns a two
characters file size which is a bit more convenient to read.
<?php
function human_filesize($bytes, $decimals = 2) {
$factor = floor((strlen($bytes) - 1) / 3);
if ($factor > 0) $sz = 'KMGT';
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor - 1] .
'B';
}
print human_filesize(12, 0); // 12B
print human_filesize(1234567890, 4); // 1.1498GB
print human_filesize(123456789, 1); // 117.7MB
print human_filesize(12345678901234, 5); // 11.22833TB
print human_filesize(1234567, 3); // 1.177MB
print human_filesize(123456); // 120.56KB
?>
I removed the P units because strlen doesn't seem to work as expected with integers longer than
14 digits. Though it might be only my system's limitation.
----
Server IP: 72.52.91.14
Probable Submitter: 95.59.167.105
----
Manual Page -- http://php.net/manual/en/function.filesize.php
Edit -- https://master.php.net/note/edit/120250
Del: integrated -- https://master.php.net/note/delete/120250/integrated
Del: useless -- https://master.php.net/note/delete/120250/useless
Del: bad code -- https://master.php.net/note/delete/120250/bad+code
Del: spam -- https://master.php.net/note/delete/120250/spam
Del: non-english -- https://master.php.net/note/delete/120250/non-english
Del: in docs -- https://master.php.net/note/delete/120250/in+docs
Del: other reasons-- https://master.php.net/note/delete/120250
Reject -- https://master.php.net/note/reject/120250
Search -- https://master.php.net/manage/user-notes.php