от байтов к новому типу преобразования

My value is slightly off could someone have a look at how i’M converting it see if there is anything wrong i’M inputting 64MB to bytes and then converting bytes to KB with FileSizeConvert and i’m not quite sure if it’s right could someone please have a look and see if there needs to be any changes for 64mb there should be 64000KB and my ouput is 65536KB.

public static function convertToBytes($from){
    $number = substr($from, 0, -2);

    switch(strtoupper(substr($from,-2))){
        case "KB":
            return $number*1024;
        case "MB":
            return $number*pow(1024,2);
        case "GB":
            return $number*pow(1024,3);
        case "TB":
            return $number*pow(1024,4);
        case "PB":
            return $number*pow(1024,5);
        default:
            return $from;
    }
}

public static function FileSizeConvert($bytes, $type)
{

    $types = ['TB', 'GB', 'MB', 'KB', 'B'];     
    $index = array_search($type, array_values($types));

    $bytes = floatval($bytes);
        $arBytes = array(
            0 => array(
                "UNIT" => "TB",
                "VALUE" => pow(1024, 4)
            ),
            1 => array(
                "UNIT" => "GB",
                "VALUE" => pow(1024, 3)
            ),
            2 => array(
                "UNIT" => "MB",
                "VALUE" => pow(1024, 2)
            ),
            3 => array(
                "UNIT" => "KB",
                "VALUE" => 1024
            ),
            4 => array(
                "UNIT" => "B",
                "VALUE" => 1
            ),
        );

    $result = $bytes / $arBytes[$index]["VALUE"];
    $result = str_replace(".", "," , strval(round($result, 2)))." ".$arBytes[$index]["UNIT"];
    return $result;     
}

использование:

$max_file_size = Helper::convertToBytes(ini_get('upload_max_filesize') . 'B'); // value is 64MB
$kb = Helper::FileSizeConvert($max_file_size, 'KB'); // ouput 65536 KB

1 ответ