Php calculates the specified folder information (number of folders, number of files, folder size)
- // Format the size of the output directory. unit: Bytes, KB, MB, and GB.
- // You can also count the number of directories.
- // Site bbs.it-home.org
- 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 );
- ?>
|