user8554
0
Q:

convert gb to bytes php

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]);
}
0
<?php 
  function tobytes($size, $type)
  {
    $types = array("B", "KB", "MB", "GB", "TB", "PB"); //You can add the rest if needed..
    
    if($key = array_search($type, $types))
      return $size * pow(1024, $key);
    else return "invalid type";
  }
  
  echo tobytes(15, "MB"); //15728640
  echo tobytes(2, "KB"); //2048
  echo tobytes(3, "w/e"); //invalid type 
?>
0

New to Communities?

Join the community