Home >php教程 >PHP源码 >最快方法计算文件大小,输出' B', ' KB', ' MB', ' GB', ' TB'

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

PHP中文网
PHP中文网Original
2016-05-26 08:21:011292browse

代码

<?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;);
?>
Statement:
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