PHP Tutorial for obtaining the file size calculation file _ PHP

Source: Internet
Author: User
PHP obtains the folder size calculation file .? PhpfunctiongetDirSize ($ dir) {$ handleopendir ($ dir); while (false! ($ FolderOrFilereaddir ($ handle) {if ($ FolderOrFile !. $ FolderOrFile !..) {If (is_dir ($ dir $ FolderOr

Function getDirSize ($ dir)
{
$ Handle = opendir ($ dir );
While (false! ==( $ FolderOrFile = readdir ($ handle )))
{
If ($ FolderOrFile! = "." & $ FolderOrFile! = "..")
{
If (is_dir ("$ dir/$ FolderOrFile "))
{
$ SizeResult + = getDirSize ("$ dir/$ FolderOrFile ");
}
Else
{
$ SizeResult + = filesize ("$ dir/$ FolderOrFile ");
}
}
}
Closedir ($ handle );
Return $ sizeResult;
}

// Automatic unit conversion function
Function getRealSize ($ size)
{
$ Kb = 1024; // Kilobyte
$ Mb = 1024 * $ kb; // Megabyte
$ Gb = 1024 * $ mb; // Gigabyte
$ Tb = 1024 * $ gb; // Terabyte

If ($ size <$ kb)
{
Return $ size. "B ";
}
Else if ($ size <$ mb)
{
Return round ($ size/$ kb, 2). "KB ";
}
Else if ($ size <$ gb)
{
Return round ($ size/$ mb, 2). "MB ";
}
Else if ($ size <$ tb)
{
Return round ($ size/$ gb, 2). "GB ";
}
Else
{
Return round ($ size/$ tb, 2). "TB ";
}
}

Echo getRealSize (getDirSize (dirname ($ _ SERVER [SCRIPT_FILENAME])./include /));


?>

######################################## #################

// Function dirsize ($ dir)
//{
// $ Handle = opendir ($ dir );
// $ Size = 0;
// While ($ file = readdir ($ handle ))
//{
// If ($ file = ".") | ($ file = "..") continue;
// If (is_dir ("$ dir/$ file "))
// $ Size + = dirsize ("$ dir/$ file ");
// Else
// $ Size + = filesize ("$ dir/$ file ");
//}
// Closedir ($ handle );
// Return $ size;
//}
// $ Big = dirsize (dirname ($ _ SERVER [SCRIPT_FILENAME]). "/");
// Echo $ big;

The result is two digits after the decimal point.

$ Big * 1024 is measured in KB.

Http://www.bkjia.com/PHPjc/486551.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/486551.htmlTechArticle? Php function getDirSize ($ dir) {$ handle = opendir ($ dir); while (false! ==( $ FolderOrFile = readdir ($ handle) {if ($ FolderOrFile! =. $ FolderOrFile! =...) {If (is_dir ($ dir/$ FolderOr...

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.