gforge-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gforge-commits] gforge/common/include utils.php,1.31,1.32


From: tom
Subject: [Gforge-commits] gforge/common/include utils.php,1.31,1.32
Date: Wed, 30 Jun 2004 13:11:54 -0500

Update of /cvsroot/gforge/gforge/common/include
In directory db.perdue.net:/tmp/cvs-serv2698/common/include

Modified Files:
        utils.php 
Log Message:
Applied patch #808: Human-Readable Filesizes

Index: utils.php
===================================================================
RCS file: /cvsroot/gforge/gforge/common/include/utils.php,v
retrieving revision 1.31
retrieving revision 1.32
diff -u -d -r1.31 -r1.32
--- utils.php   9 Mar 2004 10:16:43 -0000       1.31
+++ utils.php   30 Jun 2004 18:11:51 -0000      1.32
@@ -775,4 +775,48 @@
 
 }
 
+
+/**
+ * human_readable_bytes() - Translates an integer representing bytes to a 
human-readable format.
+ *
+ * Format file size in a human-readable way
+ * such as "xx Megabytes" or "xx Mo"
+ *
+ * @author           Andrea Paleni <andreaSPAMLESS_AT_SPAMLESScriticalbit.com>
+ * @version        1.0
+ * @param int       bytes   is the size
+ * @param bool     base10  enable base 10 representation, otherwise
+ *                 default base 2  is used  
+ * @param int       round   number of fractional digits
+ * @param array     labels  strings associated to each 2^10 or
+ *                  10^3(base10==true) multiple of base units
+ */
+function human_readable_bytes ($bytes, $base10=false, $round=0,
+                                 $labels=array(' bytes',  ' KB', ' MB', ' 
GB')) {
+  
+  if (($bytes <= 0) ||
+      (! is_array($labels)) ||
+      (count($labels) <= 0))
+    return null;
+  
+  $step = $base10 ? 3 : 10 ;
+  $base = $base10 ? 10 : 2;
+  
+  $log = (int)(log10($bytes)/log10($base));
+  
+  krsort($labels);
+  
+  foreach ($labels as $p=>$lab) {
+    $pow = $p * $step;
+    if ($log < $pow) continue;
+    $text = round($bytes/pow($base,$pow),$round) . $lab;
+    break;
+  }
+  
+   return $text;
+}
+
+
+
+
 ?>





reply via email to

[Prev in Thread] Current Thread [Next in Thread]