Php: how to obtain the code of the folder size Program

Source: Internet
Author: User
This article will show you how to get the folder size in php. we will recursively list the files and use filesize to calculate the file size and display it. The code is as follows: Copy the code? Ph...

This article will show you how to get the folder size in php. we will recursively list the files and use filesize to calculate the file size and display it.

The code is as follows:


// Get the folder size
Function dir_size ($ dir ){
If (! Preg_match ('#/$ #', $ dir )){
$ Dir. = '/';
}
$ Totalsize = 0;
// Call the file list
Foreach (get_file_list ($ dir) as $ name ){
$ Totalsize + = (@ is_dir ($ dir. $ name )? Dir_size ("$ dir $ name /"):
(Int) @ filesize ($ dir. $ name ));
}
Return $ totalsize;
}

// Obtain the file list
Function get_file_list ($ path ){
$ F = $ d = array ();
// Obtain all objects
Foreach (get_all_files ($ path) as $ name ){
If (@ is_dir ($ path. $ name )){
$ D [] = $ name;
} Else if (@ is_file ($ path. $ name )){
$ F [] = $ name;
}
}
Natcasesort ($ d );
Natcasesort ($ f );
Return array_merge ($ d, $ f );
}

// Obtain all objects
Function get_all_files ($ path ){
$ List = array ();
If ($ hndl = @ opendir ($ path) === false ){
Return $ list;
}
While ($ file = readdir ($ hndl ))! = False ){
If ($ file! = '.' & $ File! = '..'){
$ List [] = $ file;
}
}
Closedir ($ hndl );
Return $ list;
}

// Conversion Unit
Function setupSize ($ fileSize ){
$ Size = sprintf ("% u", $ fileSize );
If ($ size = 0 ){
Return ("0 Bytes ");
}
$ Sizename = array ("Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB ", "YB ");
Return round ($ size/pow (1024, ($ I = floor (log ($ size, 1024), 2). $ sizename [$ I];
}

// Directory
$ Path = './test_dir /';

// Display the file list
Print_r (get_file_list ($ path )).'
';

// Display the file size
Echo dir_size ($ path ).'
';

// Display the size of converted units
Echo setupSize (dir_size ($ path ));

?>

Dir_size () is a function used to obtain the folder size. this function uses recursive methods. you need to call get_file_list () to obtain the list of all files in the folder. if a folder exists in the file list, call get_all_files () to obtain the file list in the folder. The size of the target folder is obtained.

SetupSize () is a unit that converts the size of the uploaded object to a file unit that is easy to read. it can be converted to Bytes, KB, MB, GB, TB, PB, EB, ZB, YB, and other units.

Column 2

The code is as follows:

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.



Tutorial address:

Reprinted! But please include the article address ^

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.