Home> php教程> php手册> body text

php获取服务器所有磁盘空间大小

WBOY
Release: 2016-06-13 10:45:06
Original
3993 people have browsed it

在平常编程中有时需要获取磁盘空间使用情况,大部分情况都是使用disk_free_space和disk_total_space函数。

下面实例就是获取服务器所有磁盘空间大小,实例如下:

/**

* 字节格式化 把字节数格式为B K M G T P E Z Y 描述的大小

* @param int $size 大小

* @param int $dec 显示类型

* @return int

*/

function byte_format($size,$dec=2)

{

$a = array("B", "KB", "MB", "GB", "TB", "PB","EB","ZB","YB");

$pos = 0;

while ($size >= 1024)

{

$size /= 1024;

$pos++;

}

return round($size,$dec)." ".$a[$pos];

}

/**

* 取得单个磁盘信息

* @param $letter

* @return array

*/

function get_disk_space($letter)

{

//获取磁盘信息

$diskct = 0;

$disk = array();

/*if(@disk_total_space($key)!=NULL) *为防止影响服务器,不检查软驱

{

$diskct=1;

$disk["A"]=round((@disk_free_space($key)/(1024*1024*1024)),2)."G / ".round((@disk_total_space($key)/(1024*1024*1024)),2).'G';

}*/

$diskz = 0; //磁盘总容量

$diskk = 0; //磁盘剩余容量

$is_disk = $letter.':';

if(@disk_total_space($is_disk)!=NULL)

{

$diskct++;

$disk[$letter][0] = byte_format(@disk_free_space($is_disk));

$disk[$letter][1] = byte_format(@disk_total_space($is_disk));

$disk[$letter][2] = round(((@disk_free_space($is_disk)/(1024*1024*1024))/(@disk_total_space($is_disk)/(1024*1024*1024)))*100,2).'%';

$diskk+=byte_format(@disk_free_space($is_disk));

$diskz+=byte_format(@disk_total_space($is_disk));

}

return $disk; www.2cto.com

}

/**

* 取得磁盘使用情况

* @return var

*/

function get_spec_disk($type='system')

{

$disk = array();

switch ($type)

{

case 'system':

//strrev(array_pop(explode(':',strrev(getenv_info('SystemRoot')))));//取得系统盘符

$disk = get_disk_space(strrev(array_pop(explode(':',strrev(getenv('SystemRoot'))))));

break;

case 'all':

foreach (range('b','z') as $letter)

{

$disk = array_merge($disk,get_disk_space($letter));

}

break;

default:

$disk = get_disk_space($type);

break;

}

return $disk;

}

由此你可以扩展你一下,如获取整体磁盘空间大小,磁盘余下空间。

在这里也得重新说明一下,如果服务器有软盘记得要避开,否则会对服务器相关的软盘运行程序有定的阻碍,使用程序读取所有磁盘使用情况可能速度比较慢!注意!

source:php.cn
Statement of this Website
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
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!