Php: getting all disk space on the server _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php obtains the size of all disk space on the server. 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 shows how to obtain the disk space usage of all the disk spaces on the server. 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!

Bytes. The following instance is used to retrieve all the server disks that are empty...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.