php使用递归计算目录大小,主要使用filesize函数配合递归函数的方法来实现,具体实现源代码如下:
<?php /* 作者: http://www.manongjc.com/article/28.html */ function directory_size($directory) { $directorySize=0; if ($dh = @opendir($directory)) { while (($filename = readdir ($dh))) { if ($filename != "." && $filename != "..") { if (is_file($directory."/".$filename)){ $directorySize += filesize($directory."/".$filename); } if (is_dir($directory."/".$filename)){ $directorySize += directory_size($directory."/".$filename); } } } } @closedir($dh); return $directorySize; } $directory = "./"; $totalSize = round((directory_size($directory) / 1024), 2); echo "Directory $directory: ".$totalSize. "kb."; ?>