Php statistics function for getting folder information
-
// Collect information about folders
- // Count the number of directories
- // Format the size of the output directory. unit: Bytes, KB, MB, and GB.
- Function getFolderSize ($ 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 = getFolderSize ($ 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;
- }
-
// Format the output information
- 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 ";
- } Bbs.it-home.org
- Else
- {
- $ Size = round ($ size/(1024x1024*1024), 1 );
- Return $ size. "GB ";
- }
- }
- $ Path = "/var/www ";
- $ Ar = getFolderSize ($ path );
- Echo "the path you are viewing: $ path ";
- Echo "directory size:". sizeFormat ($ ar ['size'])."
";
- Echo "file count:". $ ar ['count']."
";
- Echo "directory count:". $ ar ['dircount']."
";
-
// Output
- // Print_r ($ ar );
- ?>
Example 2: php function for obtaining the folder size
- // Get the folder size
- 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 ('directory path '));
- ?>
|