The code is as follows: |
Copy code |
<? Php // 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). '<br> '; // Display the file size Echo dir_size ($ path). '<br> '; // 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: |
Copy code |
<? Php 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.