diff options
author | Kevin Mandura <webmaster@kevin-mandura.de> | 2024-03-11 00:06:28 +0100 |
---|---|---|
committer | Kevin Mandura <webmaster@kevin-mandura.de> | 2024-03-11 00:06:28 +0100 |
commit | 1290682ea21c3e8c8d90cce02e2387aed2189129 (patch) | |
tree | e9552352225aea4c78843fea149cf61c9553ec0f | |
parent | 473492db008592fe4614d56023d47a1b5fd9f1e0 (diff) | |
download | ktls-website-1290682ea21c3e8c8d90cce02e2387aed2189129.tar.gz ktls-website-1290682ea21c3e8c8d90cce02e2387aed2189129.zip |
Add convertToBytes function
-rw-r--r-- | includes/functions/convertToBytes.inc.php | 29 |
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]); +} |