首页 > 代码库 > 获取windows磁盘的可用空间函数

获取windows磁盘的可用空间函数

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

 

获取windows磁盘的可用空间函数