---
title: "格式化文件大小"
author: "jcyzone"
date: "2013-04-09T15:41:06Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2013-04-09T15:41:06Z"
id: 1815262
url: "https://jcyzone.iteye.com/blog/1815262"
---

function formatBytes($bytes) {  
 if($bytes &gt;= 1073741824) {  
  $bytes = round($bytes / 1073741824 * 100) / 100 . 'GB';  
 } elseif($bytes &gt;= 1048576) {  
  $bytes = round($bytes / 1048576 * 100) / 100 . 'MB';  
 } elseif($bytes &gt;= 1024) {  
  $bytes = round($bytes / 1024 * 100) / 100 . 'KB';  
 } else {  
  $bytes = $bytes . 'Bytes';  
 }  
 return $bytes;  
}

/\**  
 * 字节格式化 把字节数格式为 B K M G T 描述的大小  
 * @return string  
 \*/  
function byte\_format($size, $dec=2) {  
 $a = array("B", "KB", "MB", "GB", "TB", "PB");  
 $pos = 0;  
 while ($size &gt;= 1024) {  
   $size /= 1024;  
     $pos++;  
 }  
 return round($size,$dec)." ".$a\[$pos];  
}