道者编程

PHP换算文件大小

//换算文件大小
function formatBytes($bytes) 
{
	if($bytes >= 1073741824) {
		$bytes = round($bytes / 1073741824 * 100) / 100 . 'GB';
	} elseif($bytes >= 1048576) {
		$bytes = round($bytes / 1048576 * 100) / 100 . 'MB';
	} elseif($bytes >= 1024) {
		$bytes = round($bytes / 1024 * 100) / 100 . 'KB';
	} else {
		$bytes = $bytes . 'Bytes';
	}
	return $bytes;
}



//换算文件大小byte
function bytes($str)
{
	if(strstr($str,'KB')){
		$s = str_replace('KB','',$str);
		$byte= $s*1024;
		
	} elseif(strstr($str,'MB')){
		$s = str_replace('MB','',$str);
		$byte = $s*1048576;
	}elseif(strstr($str,'GB')){
		$s = str_replace('GB','',$str);
		$byte = $s*1073741824;
	}
	return $byte;
}
第一个把字节转换为K,M,G,第二个刚好相反。

最新评论:
我要评论:

看不清楚