In common programming, you sometimes need to obtain disk space usage. In most cases, the disk_free_space and disk_total_space functions are used. The following example gets the size of all disk space on the server. the instance format is as follows:/*** format the number of bytes to BKMGT... SyntaxHighlighter. all ();
In common programming, you sometimes need to obtain disk space usage. In most cases, the disk_free_space and disk_total_space functions are used.
The following instance obtains the disk space of all servers. the instance is as follows:
/**
* In byte format, the number of bytes is in the format of B K M G T P E Z Y.
* @ Param int $ size
* @ Param int $ dec display type
* @ 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];
}
/**
* Obtain information about a single disk
* @ Param $ letter
* @ Return array
*/
Function get_disk_space ($ letter)
{
// Obtain disk information
$ Diskct = 0;
$ Disk = array ();
/* If (@ disk_total_space ($ key )! = NULL) * to prevent server impact, do not check the disk drive.
{
$ 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; // total disk capacity
$ Diskk = 0; // remaining disk capacity
$ 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 ). '% ';
$ Diskk + = byte_format (@ disk_free_space ($ is_disk ));
$ Diskz + = byte_format (@ disk_total_space ($ is_disk ));
}
Return $ disk; www.2cto.com
}
/**
* Obtain disk usage
* @ Return var
*/
Function get_spec_disk ($ type = 'system ')
{
$ Disk = array ();
Switch ($ type)
{
Case 'system ':
// Strrev (array_pop (explode (':', strrev (getenv_info ('systemroot'); // Obtain the system drive letter
$ 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;
}
You can expand the disk space, such as obtaining the overall disk space and remaining disk space.
Here, we have to re-explain that if the server has a floppy disk, remember to avoid it. Otherwise, it will hinder the program running on the disk, the usage of the program to read all disks may be slow! Note!