convert 20M into KB php code example
Example 1: php convert mb to bytes
function toByteSize($p_sFormatted) {
$aUnits = array('B'=>0, 'KB'=>1, 'MB'=>2, 'GB'=>3, 'TB'=>4, 'PB'=>5, 'EB'=>6, 'ZB'=>7, 'YB'=>8);
$sUnit = strtoupper(trim(substr($p_sFormatted, -2)));
if (intval($sUnit) !== 0) {
$sUnit = 'B';
}
if (!in_array($sUnit, array_keys($aUnits))) {
return false;
}
$iUnits = trim(substr($p_sFormatted, 0, strlen($p_sFormatted) - 2));
if (!intval($iUnits) == $iUnits) {
return false;
}
return $iUnits * pow(1024, $aUnits[$sUnit]);
}
Example 2: convert gb to bytes php
<?php
function tobytes($size, $type)
{
$types = array("B", "KB", "MB", "GB", "TB", "PB");
if($key = array_search($type, $types))
return $size * pow(1024, $key);
else return "invalid type";
}
echo tobytes(15, "MB");
echo tobytes(2, "KB");
echo tobytes(3, "w/e");
?>