PHP convert KB MB GB TB etc to Bytes

后端 未结 8 2492
长发绾君心
长发绾君心 2020-12-16 18:53


I\'m asking how to convert KB MB GB TB & co. into bytes.
For example:

byteconvert(\"10KB\") // => 10240
byteconvert(\"10.5KB\") // =>          


        
8条回答
  •  清歌不尽
    2020-12-16 19:38

    I know this is a relative old topic, but here's a function that I sometimes have to use when I need this kind of stuff; You may excuse for if the functions dont work, I wrote this for hand in a mobile:

    function intobytes($bytes, $stamp = 'b') {
        $indx = array_search($stamp, array('b', 'kb', 'mb', 'gb', 'tb', 'pb', 'yb'));
        if ($indx > 0) {
            return $bytes * pow(1024, $indx);
        }
        return $bytes;
    }
    

    and as compact

    function intobytes($bytes, $stamp='b') {$indx=array_search($stamp,array('b','kb','mb','gb','tb','pb','yb'));if($indx > 0){return $bytes * pow(1024,$indx);} return $bytes;}
    

    Take care!

    Brodde85 ;)

提交回复
热议问题