about summary refs log tree commit diff
diff options
context:
space:
mode:
authorKevin Mandura <webmaster@kevin-mandura.de>2024-03-11 00:06:28 +0100
committerKevin Mandura <webmaster@kevin-mandura.de>2024-03-11 00:06:28 +0100
commit1290682ea21c3e8c8d90cce02e2387aed2189129 (patch)
treee9552352225aea4c78843fea149cf61c9553ec0f
parent473492db008592fe4614d56023d47a1b5fd9f1e0 (diff)
downloadktls-website-1290682ea21c3e8c8d90cce02e2387aed2189129.tar.gz
ktls-website-1290682ea21c3e8c8d90cce02e2387aed2189129.zip
Add convertToBytes function
-rw-r--r--includes/functions/convertToBytes.inc.php29
1 files changed, 29 insertions, 0 deletions
diff --git a/includes/functions/convertToBytes.inc.php b/includes/functions/convertToBytes.inc.php
new file mode 100644
index 0000000..f2d7f2d
--- /dev/null
+++ b/includes/functions/convertToBytes.inc.php
@@ -0,0 +1,29 @@
+<?php
+
+/**
+ *
+ * Convert unit to bytes
+ *
+ * Input number with the unit character (K, M, G, T) at the end to
+ * convert it to bytes.  If no unit character is specified, it is
+ * assumed, that the number is already specified in bytes.
+ *
+ * @param     int  $inputSize  Input number containing optional unit character
+ * @return    int  Raw bytes value
+ *
+ */
+function convertToBytes($inputSize) {
+    $aUnits = array('bytes' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4);
+    $sUnit = strtoupper(trim(substr($inputSize, -2)));
+    if (intval($sUnit) !== 0) {
+        $sUnit = 'bytes';
+    }
+    if (!in_array($sUnit, array_keys($aUnits))) {
+        return false;
+    }
+    $iUnits = trim(substr($inputSize, 0, strlen($inputSize) - 2));
+    if (!intval($iUnits) == $iUnits) {
+        return false;
+    }
+    return $iUnits * pow(1024, $aUnits[$sUnit]);
+}