Home > php教程 > PHP源码 > body text

最快方法计算文件大小,输出' B', ' KB', ' MB', ' GB', ' TB'

PHP中文网
Release: 2016-05-26 08:21:01
Original
1202 people have browsed it

代码

<?php
date_default_timezone_set(&#39;UTC&#39;);

function format_bytes($size)
{ 
	$arr = array(&#39; B&#39;, &#39; KB&#39;, &#39; MB&#39;, &#39; GB&#39;, &#39; TB&#39;); 
	for ($f = 0; $size >= 1024 && $f < 4; $f++)
	{
		$size /= 1024; 
	}
	return round($size, 2).$arr[$f]; 
}
echo format_bytes(&#39;123456789&#39;);
?>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template