• 0
  • 0

跨端统计各平台硬盘使用情况

2019-08-21 854 0 admin 所属分类:PHP 记录
// $data = get_spec_disk('/www/data/attachment');
// debug($data);

function isWin() {
	return strpos(strtolower(PHP_OS), 'win') === 0;
}


// 取得磁盘使用情况
function get_spec_disk($type = 'system') {
	$info = [];
	$info['count'] = 0;
	$info['hd'] = [];
	$info['all'] = 0;
	$info['used'] = 0;
	$info['reset'] = 0;
	$info['percent'] = 0;

	if (isWin()) {
		switch ($type) {
		case 'system':
			$info['hd'][] = get_disk_space(strrev(array_pop(explode(':', strrev(getenv('SystemRoot'))))));
			break;
		case 'all':
			foreach (range('b', 'z') as $letter) {
				$temp = get_disk_space($letter);
				if ($temp) {
					$info['hd'][] = $temp;
				}
			}
			break;
		default:
			$info['hd'][] = get_disk_space($type);
			break;
		}

		$info['count'] = count($info['hd']);
		foreach ($info['hd'] as $k => $v) {
			$info['all'] += str_replace(array('G','g'),'',$v['all']);
			$info['used'] += str_replace(array('G','g'),'',$v['all'] - $v['reset']);
			$info['reset'] += str_replace(array('G','g'),'',$v['reset']);
		}
		if ($info['all']) {
			$info['percent'] = round($info['used']/$info['all'],2);
		}
	} else {
		if (is_dir($type)) {
			$info['hd'][] = get_disk_space($type);
			$info['count'] = 1;
			foreach ($info['hd'] as $k => $v) {
				$info['all'] += str_replace(array('G','g'),'',$v['all']);
				$info['used'] += str_replace(array('G','g'),'',$v['all'] - $v['reset']);
				$info['reset'] += str_replace(array('G','g'),'',$v['reset']);
			}
			if ($info['all']) {
				$info['percent'] = round($info['used']/$info['all'],2);
			}
		} else {
			$fp = popen('df -lh | grep -E "^(/)"','r');
			if (!$fp){
				exit('请开启popen函数');
			}
			$rs = fread($fp,1024);
			pclose($fp);
			$rs = preg_replace("/\s{2,}/",' ',$rs);
			$hds = array_filter(explode("\n",$rs));
			$info['count'] = count($hds);
			foreach ($hds as $k => $v) {
				$hd = explode(" ",$v);
				$info['hd'][$k]['file'] = $hd[0];
				$info['hd'][$k]['all'] = $hd[1];
				$info['hd'][$k]['used'] = $hd[2];
				$info['hd'][$k]['reset'] = $hd[3];
				$info['hd'][$k]['percent'] = $hd[4];
				$info['hd'][$k]['path'] = $hd[5];
				$info['all'] += str_replace(array('G','g'),'',$hd[1]);
				$info['used'] += str_replace(array('G','g'),'',$hd[2]);
				$info['reset'] += str_replace(array('G','g'),'',$hd[3]);
			}
		}	
		
		
		if ($info['all']) {
			$info['percent'] = round($info['used']/$info['all'],2);
		}
	}
	
	return $info;
}

// 取得单个磁盘信息
function get_disk_space($letter) {
	//获取磁盘信息
	$diskct = 0;
	$disk = array();
	if (is_dir($letter)) {
		$is_disk = $letter;
	} else {
		$is_disk = $letter . ':';
	}
	if (@disk_total_space($is_disk) != NULL) {
		$diskct++;
		$disk['reset'] = byte_format(@disk_free_space($is_disk));
		$disk['all'] = byte_format(@disk_total_space($is_disk));
		$disk['used'] = byte_format(@disk_total_space($is_disk) - @disk_free_space($is_disk));
		$disk['percent'] = round(((@disk_free_space($is_disk) / (1024 * 1024 * 1024)) / (@disk_total_space($is_disk) / (1024 * 1024 * 1024))) * 100, 2) . '%';
		$disk['path'] = $letter;
	}
	return $disk;
}


function byte_format($size, $dec = 2) {
	$size = $size/(1024*1024*1024);
	return round($size,$dec)." G";
}


返回顶部