<?php /* *获取某个磁盘的剩余空间 *$param 关联数组,下标是哪个盘,单位,可以是B,KB,MB,GB *可以设置获取多个磁盘,例如:array('C'=>'KB','D'=>'MB','E'=>'GB','F'=>'B') *假如出错,返回false */ function Space($arr){ //检查参数 if(is_array($arr)){ //初始化存储值 $memory = array(); foreach($arr as $disk=>$size){ $D = strtoupper($disk).':';//转化为大写的键盘路径 $S = strtoupper($size);//转变为大写的单位 if(in_array($D,array('C:','D:','E:','F:')) && in_array($S,array('B','KB','MB','GB','TB'))){ switch($S){ case 'B': $memory[$disk]= disk_free_space($D).'B'; break; case 'KB': $d = round(disk_free_space($D) / 1024); $memory[$disk] = $d .'KB'; break; case 'MB': $d = round(disk_free_space($D) / pow(1024,2)); $memory[$disk]= $d.'MB'; break; case 'GB': $d = round(disk_free_space($D) / pow(1024,3)); $memory[$disk]= $d.'GB'; break; case 'TB': $d = sprintf("%.4f", disk_free_space($D) / pow(1024,4)); $memory[$disk] = $d .'TB'; break; default: return 0; break; } }else{ return null; } } return $memory; }else{ return null; } } $arr = array('c'=>'kb','d'=>'Mb','e'=>'Gb','f'=>'Tb'); var_dump(Space($arr));
根据上面获取磁盘可用空间,我们可以利用电脑的定时器进行磁盘监控,并报警磁盘的剩余空间应急
//要报警的磁盘和设置的单位 $di = array('c'=>'gb','d'=>'gb','e'=>'gb','f'=>'gb'); $_space = Space($di); //应急配置 $alarm = '50GB'; $data = array(); //报警处理 foreach ($_space as $k=>$v){ //当磁盘空间小于50GB的情况 if(intval(substr($v,0,-2)) <= intval(substr($alarm,0,-2))){ $data[] = array('data'=>$k.'磁盘空间不足'.$v.',请尽快处理'); } } echo json_encode($data,JSON_UNESCAPED_UNICODE);