Provides various official and user-released code examples. For code reference, you are welcome to learn how to calculate the folder size, including subfolders, formatting the Output Folder size, number of files, and number of subfolders.
// The code can also be used to count the number of directories
// Format the size of the output directory. Unit: Bytes, KB, MB, and GB.
Function getDirectorySize ($ path)
{
$ Totalsize = 0;
$ Totalcount = 0;
$ Dircount = 0;
If ($ handle = opendir ($ path ))
{
While (false! ==( $ File = readdir ($ handle )))
{
$ Nextpath = $ path. '/'. $ file;
If ($ file! = '.' & $ File! = '..'&&! Is_link ($ nextpath ))
{
If (is_dir ($ nextpath ))
{
$ Dircount ++;
$ Result = getDirectorySize ($ nextpath );
$ Totalsize + = $ result ['SIZE'];
$ Totalcount + = $ result ['Count'];
$ Dircount + = $ result ['dircount'];
}
Elseif (is_file ($ nextpath ))
{
$ Totalsize + = filesize ($ nextpath );
$ Totalcount ++;
}
}
}
}
Closedir ($ handle );
$ Total ['SIZE'] = $ totalsize;
$ Total ['Count'] = $ totalcount;
$ Total ['dircount'] = $ dircount;
Return $ total;
}
Function sizeFormat ($ size)
{
$ SizeStr = '';
If ($ size <1024)
{
Return $ size. "bytes ";
}
Else if ($ size <(1024*1024 ))
{
$ Size = round ($ size/1024,1 );
Return $ size. "KB ";
}
Else if ($ size <(1024*1024*1024 ))
{
$ Size = round ($ size/(1024*1024), 1 );
Return $ size. "MB ";
}
Else
{
$ Size = round ($ size/(1024x1024*1024), 1 );
Return $ size. "GB ";
}
}
$ Path = "/home/www/htdocs ";
$ Ar = getDirectorySize ($ path );
Echo "path: $ path ";
Echo "directory size:". sizeFormat ($ ar ['SIZE'])."
";
Echo "file count:". $ ar ['Count']."
";
Echo "Directory:". $ ar ['dircount']."
";
// Print_r ($ ar );
?>
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB