本文章向大家介紹php如何計算某個目錄的大小(多少kb,多少兆m),主要使用filesize函數配合遞歸函數的方法來實現,需要的朋友可以參考一下本文章的源代碼。
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."; ?>