Calculates the size of the folder, including subfolders, formatting the output folder size, number of files, and the number of subfolders.
The code is as follows |
|
? Code can also be used to statistic the number of directories Format output Directory size unit: BYTES,KB,MB,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; } |
The size of the computed file directory in PHP is essentially the "filesize" function, which computes the size of each file recursively, and then calculates their size and that is the entire file directory.
Because the file size that is returned directly is in bytes, we generally have to convert to the size we usually get, and here is the function of the unit conversion:
The code is as follows |
|
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/(1024*1024*1024), 1); return $size. " GB "; } } $path = "/home/www/htdocs"; $ar =getdirectorysize ($path); echo "echo "Directory Size:". Sizeformat ($ar [' size ']). " <br> "; echo "Number of files:". $ar [' count ']. <br> "; echo "directory:". $ar [' Dircount ']. " <br> "; Print_r ($ar); ?> |
followed by a unit function
The function of the most important is based on the number of bytes in the file, Judge should select the unit of statistics, that is, a file in a certain unit such as MB, then the file must be less than 1GB, or, of course, to use GB as a unit, and the file is greater than KB, or the smaller The function code is as follows
The code is as follows |
|
Size () Statistics file sizes function Size ($byte) { if ($byte < 1024) { $unit = "B"; } else if ($byte < 10240) { $byte =ROUND_DP ($byte/1024, 2); $unit = "KB"; } else if ($byte < 102400) { $byte =ROUND_DP ($byte/1024, 2); $unit = "KB"; } else if ($byte < 1048576) { $byte =ROUND_DP ($byte/1024, 2); $unit = "KB"; } else if ($byte < 10485760) { $byte =ROUND_DP ($byte/1048576, 2); $unit = "MB"; } else if ($byte < 104857600) { $byte =ROUND_DP ($byte/1048576,2); $unit = "MB"; } else if ($byte < 1073741824) { $byte =ROUND_DP ($byte/1048576, 2); $unit = "MB"; } else { $byte =ROUND_DP ($byte/1073741824, 2); $unit = "GB"; } $byte. = $unit; return $byte; } function ROUND_DP ($num, $DP) { $sh = POW ($DP); Return (Round ($num * $sh)/$sh); } |
About PHP round function usage can refer to http://www.111cn.net/w3school/php/func_math_round.htm