说明:
在DedeCMS后台添加“空间使用量”和“数据库使用量”。方便随时了解空间和MySQL的占用情况。
需要编辑以下文件:
include\extend.func.php(添加几函数)
dede\templets\index_body.htm (后台模板)
dede\space.js.php(这个文件是新添加的)
效果:
操作:
第一步:在 include\extend.func.php 中添加几个函数:
//获取文件夹大小 function GetDirSize($dir){ $handle = opendir($dir); $sizeResult = ''; $FolderOrFile = readdir($handle); while($FolderOrFile !== false) { if($FolderOrFile != '.' && $FolderOrFile != '..') { if(is_dir("$dir/$FolderOrFile")) { $sizeResult += GetDirSize("$dir/$FolderOrFile"); } else { $sizeResult += filesize("$dir/$FolderOrFile"); } } } closedir($handle); if(empty($sizeResult)) $sizeResult = 0; return $sizeResult; } //获取文件夹大小(upall专用) function GetFolderSize($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 += GetFolderSize($directory . "/" . $fileName); } } } @closedir($dh); return $directorySize; } // 数据库使用量 function GetMysqlSize(){ global $dsql,$cfg_dbname; $dbName = $cfg_dbname; $sql = "SELECT table_schema AS db, " . "sum( data_length + index_length ) /1024 / 1024 AS total " . "FROM information_schema.TABLES " . "WHERE table_schema = '$dbName' " . "GROUP BY table_schema " . "LIMIT 1"; $mysqlUseRs = $dsql->GetOne($sql); return $mysqlUseRs['total']; } //查看数据大小 function GetRealSize($size){ $kb = 1024; // Kilobyte $mb = 1024 * $kb; // Megabyte $gb = 1024 * $mb; // Gigabyte $tb = 1024 * $gb; // Terabyte if($size < $kb){ return round($size,2).'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'; } }
第二步:在 dede\templets\index_body.htm 中添加数据显示:
<tr> <td class='nline' style="text-align:right"> 空间使用量:</td> <td class='nline' style="text-align:left"> <a href="javascript:;" title="点击查看" id="folderSize">[点击查看]</a> <script type="application/javascript"> $('#folderSize').click(function(){ $(this).html('正在查询..'); $.get("space.js.php", function(data){ $('#folderSize').html(data); }) }); </script> </td> </tr> <tr> <td class='nline' style="text-align:right"> 数据库使用量:</td> <td class='nline' style="text-align:left"> <?php echo GetRealSize(GetMysqlSize());?></td> </tr>
加到这些代码后边:
<tr> <td class='nline' style="text-align:right"> 软件版本信息: </td> <td class='nline' style="text-align:left"> <?php echo $cfg_soft_enname; ?> <?php echo $cfg_version; ?></td> </tr>
第三步:创建 dede\space.js.php :
<?php // 输出网站所占用的磁盘空间 require_once(dirname(__FILE__)."/config.php"); echo $spaceUse = GetRealSize(GetFolderSize('../')); ?>
<完>