use recursive traversal instead of scandir function

This commit is contained in:
printempw 2016-03-19 12:56:31 +08:00
parent f9f6b5168a
commit 148b03b512
2 changed files with 35 additions and 10 deletions

View File

@ -3,7 +3,7 @@
* @Author: printempw
* @Date: 2016-02-03 14:39:50
* @Last Modified by: printempw
* @Last Modified time: 2016-03-19 10:08:11
* @Last Modified time: 2016-03-19 12:53:18
*/
require "../includes/session.inc.php";
if (!$user->is_admin) header('Location: ../index.php?msg=看起来你并不是管理员');
@ -36,7 +36,7 @@ $db = new Database\Database();
<span class="info-box-icon bg-green"><i class="fa fa-files-o"></i></span>
<div class="info-box-content">
<span class="info-box-text">上传材质总数</span>
<span class="info-box-number"><?php echo count(scandir("../textures/"))-2;?></span>
<span class="info-box-number"><?php echo Utils::getFileNum(BASE_DIR."/textures/");?></span>
</div><!-- /.info-box-content -->
</div><!-- /.info-box -->
@ -44,7 +44,7 @@ $db = new Database\Database();
<span class="info-box-icon bg-yellow"><i class="fa fa-hdd-o"></i></span>
<div class="info-box-content">
<span class="info-box-text">占用空间大小</span>
<span class="info-box-number"><?php echo floor(Utils::getDirSize("../textures/")/1024)."KB";?></span>
<span class="info-box-number"><?php echo floor(Utils::getDirSize(BASE_DIR."/textures/")/1024)."KB";?></span>
</div><!-- /.info-box-content -->
</div><!-- /.info-box -->
</div>

View File

@ -3,7 +3,7 @@
* @Author: printempw
* @Date: 2016-01-16 23:01:33
* @Last Modified by: printempw
* @Last Modified time: 2016-03-19 09:52:51
* @Last Modified time: 2016-03-19 12:55:08
*/
class Utils
@ -73,22 +73,47 @@ class Utils
* @return int, total size in bytes
*/
public static function getDirSize($dir) {
$dh = opendir($dir);
$resource = opendir($dir);
$size = 0;
while(false !== ($file = @readdir($dh))) {
if ($file!='.' && $file!='..') {
$path = $dir.'/'.$file;
while($filename = readdir($resource)) {
if ($filename != "." && $filename != "..") {
$path = $dir.$filename;
if (is_dir($path)) {
$size += $this->getDirSize($path);
// recursion
$size += self::getDirSize($path);
} else if (is_file($path)) {
$size += filesize($path);
}
}
}
closedir($dh);
closedir($resource);
return $size;
}
/**
* Recursively count files of specified directory
*
* @param string $dir
* @param $file_num
* @return int, total size in bytes
*/
public static function getFileNum($dir, $file_num = 0) {
$resource = opendir($dir);
while($filename = readdir($resource)) {
if ($filename != "." && $filename != "..") {
$path = $dir.$filename;
if (is_dir($path)) {
// recursion
$file_num = self::getFileNum($path, $file_num);
} else {
$file_num++;
}
}
}
closedir($resource);
return $file_num;
}
/**
* Simple SQL injection protection
*