summaryrefslogtreecommitdiffstats
path: root/application/helpers/filebin_helper.php
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2012-06-22 15:43:57 +0200
committerFlorian Pritz <bluewind@xinu.at>2012-06-22 15:47:13 +0200
commitce4cfd248b4c1c0d044f607804da6b4b312aa239 (patch)
treea39554e0c0fe9aeb6174c5598667e77b0bbbfcab /application/helpers/filebin_helper.php
parentd9947c840bbce291c1cf3f0cb73f0c343686892e (diff)
format_bytes(): fix bug if size=0
If $size is 0 log() will return -INF leading to an "undefined offset" error when trying to get the suffix. We fix this by copying the code from fb-client which handles this issue correctly and will also work for sizes above the biggest suffix (won't happen here, but who cares). Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'application/helpers/filebin_helper.php')
-rw-r--r--application/helpers/filebin_helper.php23
1 files changed, 18 insertions, 5 deletions
diff --git a/application/helpers/filebin_helper.php b/application/helpers/filebin_helper.php
index df4a453f7..f19438a1c 100644
--- a/application/helpers/filebin_helper.php
+++ b/application/helpers/filebin_helper.php
@@ -1,10 +1,23 @@
<?php
-// Source: http://ibnuyahya.com/format-bytes-to-kb-mb-gb/
-function format_bytes($size, $precision = 2){
- $base = log($size) / log(1024);
- $suffixes = array('B', 'KiB', 'MiB', 'GiB', 'TiB' , 'PiB' , 'EiB');
- return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
+function format_bytes($size)
+{
+ $suffixes = array('B', 'KiB', 'MiB', 'GiB', 'TiB' , 'PiB' , 'EiB', 'ZiB', 'YiB');
+ $boundary = 2048.0;
+
+ for ($suffix_pos = 0; $suffix_pos + 1 < count($suffixes); $suffix_pos++) {
+ if ($size <= $boundary && $size >= -$boundary) {
+ break;
+ }
+ $size /= 1024.0;
+ }
+
+ # don't print decimals for bytes
+ if ($suffix_pos != 0) {
+ return sprintf("%.2f%s", $size, $suffixes[$suffix_pos]);
+ } else {
+ return sprintf("%.0f%s", $size, $suffixes[$suffix_pos]);
+ }
}
// Original source: http://www.phpfreaks.com/forums/index.php?topic=198274.msg895468#msg895468