/**
* 字节转换
*
*/
private function getFilesize($num) {
$p = 0;
$format = 'bytes';
if( $num > 0 && $num < 1024 ) {
$p = 1;
return number_format($num) . ' ' . $format;
}
if( $num >= 1024 && $num < pow(2, 20) ){ #pow(2,40)是2的40次方。
$p = 10;
$format = 'KB';
}
if ( $num >= pow(2, 20) && $num < pow(2, 30) ) {
$p = 20;
$format = 'MB';
}
if ( $num >= pow(2, 30) && $num < pow(2, 40) ) {
$p = 30;
$format = 'GB';
}
if ( $num >= pow(2, 40) && $num < pow(2, 50) ) {
$p = 40;
$format = 'TB';
}
$num /= pow(2, $p);
return number_format($num, 3) . ' ' . $format;
}
php 字节数bytes转换为KB、MB、GB、TB
最新推荐文章于 2023-02-18 10:06:38 发布