wordpress 添加 显示磁盘剩余空间百分比的插件
在 wp-content/plugins 文件夹下
我取的文件名是: folder-sizes-dashboard-widget.php
在仪表盘 可以看到 Folder Sizes 标题的Box
<?php
/*
Plugin Name: Folder Sizes Dashboard Widget
Plugin URI: http://wordpress.stackexchange.com/q/67876/12615
Description: List the following folder sizes in a Dashboard Widget: Uploads dir, WP Content dir, WordPress base dir.
Observation: PHP folder size functions from this Answer: https://stackoverflow.com/a/8348396/1287812
*/
add_action( 'wp_dashboard_setup', 'wpse_67876_wp_dashboard_setup' );
function wpse_67876_wp_dashboard_setup()
{
// Admins only
if( current_user_can( 'install_plugins' ) )
wp_add_dashboard_widget( 'wpse_67876_folder_sizes', __( 'Folder Sizes' ), 'wpse_67876_wp_add_dashboard_widget' );
}
function wpse_67876_wp_add_dashboard_widget()
{
$upload_dir = wp_upload_dir();
$upload_space = wpse_67876_foldersize( $upload_dir['basedir'] );
$content_space = wpse_67876_foldersize( WP_CONTENT_DIR );
$wp_space = wpse_67876_foldersize( ABSPATH );
/* ABSOLUTE paths not being shown in Widget */
// echo '<b>' . $upload_dir['basedir'] . ' </b><br />';
echo '<i>Uploads</i>: ' . wpse_67876_format_size( $upload_space ) . '<br /><br />';
// echo '<b>' . WP_CONTENT_DIR . ' </b><br />';
echo '<i>wp-content</i>: ' . wpse_67876_format_size( $content_space ) . '<br /><br />';
if( is_multisite() )
{
echo '<i>wp-content/blogs.dir</i>: ' . wpse_67876_format_size( wpse_67876_foldersize( WP_CONTENT_DIR . '/blogs.dir' ) ) . '<br /><br />';
}
// echo '<b>' . ABSPATH . ' </b><br />';
echo '<i>WordPress</i>: ' . wpse_67876_format_size( $wp_space );
$calc_root_path = ABSPATH;
/* get disk space free (in bytes) */
$df = disk_free_space($calc_root_path);
/* and get disk space total (in bytes) */
$dt = disk_total_space($calc_root_path);
/* now we calculate the disk space used (in bytes) */
$du = $dt - $df;
/* percentage of disk used - this will be used to also set the width % of the progress bar */
$dp = sprintf('%.2f',($du / $dt) * 100);
/* and we formate the size from bytes to MB, GB, etc. */
$df = my_custom_format_size($df);
$du = my_custom_format_size($du);
$dt = my_custom_format_size($dt);
?>
<style type='text/css'>
.progress {
border: 2px solid #5E96E4;
height: 32px;
width: 540px;
margin: 30px auto;
}
.progress .prgbar {
background: #A7C6FF;
position: relative;
height: 32px;
z-index: 999;
}
.progress .prgtext {
color: #286692;
text-align: center;
font-size: 13px;
padding: 9px 0 0;
width: 540px;
position: absolute;
z-index: 1000;
}
.progress .prginfo {
margin: 3px 0;
}
</style>
<div class='progress'>
<div class='prgtext'><?php echo $dp; ?>% Disk Used</div>
<div class='prgbar' style="width:<?php echo $dp; ?>%;"></div>
<div class='prginfo'>
<span style='float: left;'><?php echo "$du of $dt used"; ?></span>
<span style='float: right;'><?php echo "$df of $dt free"; ?></span>
<span style='clear: both;'></span>
</div>
</div>
<?php
}
if(!function_exists('my_custom_format_size')){
function my_custom_format_size( $bytes )
{
$types = array( 'B', 'KB', 'MB', 'GB', 'TB' );
for( $i = 0; $bytes >= 1024 && $i < ( count( $types ) -1 ); $bytes /= 1024, $i++ );
return( round( $bytes, 2 ) . " " . $types[$i] );
}
}
function wpse_67876_foldersize( $path )
{
$total_size = 0;
$files = scandir( $path );
$cleanPath = rtrim( $path, '/' ) . '/';
foreach( $files as $t ) {
if ( '.' != $t && '..' != $t )
{
$currentFile = $cleanPath . $t;
if ( is_dir( $currentFile ) )
{
$size = wpse_67876_foldersize( $currentFile );
$total_size += $size;
}
else
{
$size = filesize( $currentFile );
$total_size += $size;
}
}
}
return $total_size;
}
function wpse_67876_format_size($size)
{
$units = explode( ' ', 'B KB MB GB TB PB' );
$mod = 1024;
for ( $i = 0; $size > $mod; $i++ )
$size /= $mod;
$endIndex = strpos( $size, "." ) + 3;
return substr( $size, 0, $endIndex ) . ' ' . $units[$i];
}
通过PHP显示 磁盘剩余空间
步骤1 - PHP代码
替换需要统计的路径calc_root_path的值
$calc_root_path = '/home/vagrant';
/* get disk space free (in bytes) */
$df = disk_free_space($calc_root_path);
/* and get disk space total (in bytes) */
$dt = disk_total_space($calc_root_path);
/* now we calculate the disk space used (in bytes) */
$du = $dt - $df;
/* percentage of disk used - this will be used to also set the width % of the progress bar */
$dp = sprintf('%.2f',($du / $dt) * 100);
/* and we formate the size from bytes to MB, GB, etc. */
$df = my_custom_format_size($df);
$du = my_custom_format_size($du);
$dt = my_custom_format_size($dt);
步骤2 - css 样式
这是进度条的样式,你可以将这个样式代码,放入到你要存放的web路径中,让 步骤1 的php代码可以引入到这个css样式
<style type='text/css'>
.progress {
border: 2px solid #5E96E4;
height: 32px;
width: 540px;
margin: 30px auto;
}
.progress .prgbar {
background: #A7C6FF;
position: relative;
height: 32px;
z-index: 999;
}
.progress .prgtext {
color: #286692;
text-align: center;
font-size: 13px;
padding: 9px 0 0;
width: 540px;
position: absolute;
z-index: 1000;
}
.progress .prginfo {
margin: 3px 0;
}
</style>
步骤3 HTML 代码
你可以选择将这短代码放入到web 目录的一个php文件中。这个HTML代码能够放入到步骤1创建的php文件或者是独立分开的文件。
<div class='progress'>
<div class='prgtext'><?php echo $dp; ?>% Disk Used</div>
<div class='prgbar'></div>
<div class='prginfo'>
<span style='float: left;'><?php echo "$du of $dt used"; ?></span>
<span style='float: right;'><?php echo "$df of $dt free"; ?></span>
<span style='clear: both;'></span>
</div>
</div>
References
wordpress 添加 显示磁盘剩余空间百分比的插件的更多相关文章
- 系统服务监控指标--load、CPU利用率、磁盘剩余空间、磁盘I/O、内存使用情况等
介绍 大型互联网企业的背后,依靠的是成千上万台服务器日夜不停的运转,以支撑其业务的运转.宕机对于互联网企业来说,代价是沉重的,轻则影响用户体验,重则直接影响交易,导致交易下跌,并且给企业声誉造成不可挽 ...
- sqlserver 出现 因为文件组 'PRIMARY' 已满 的解决办法 有可能是磁盘剩余空间不足 导致的
一般虚拟主机提供商是通过限制数据库文件的大小来实现提供定制的数据库空间的.当你把从虚拟数据库空间备份下来的文件恢复到自己的服务器上时,这个限制还是存在的.找到数据库文件 给增加个数据文件就好了 解决办 ...
- zabbix 调用python脚本监控 磁盘剩余空间(创建模版,创建监控项,创建触发器)
主要 记录一下 使用zabbix 自己创建模版.监控项.触发器,并调用python脚本. 需求: 监控备份机磁盘剩余空间(windows系统) 一.安装zabbix_agent 比较简单 修改配置文 ...
- Linux下查看磁盘剩余空间和文件夹大小
1. du -sh 查看当前文件夹大小 2. du -sh * | sort -n 列出当前文件夹下的所有文件夹及其大小,并按照文件夹大小排序 du - sh * //查看当前文件夹下所有文件的大小 ...
- SQL Server自动化运维系列——监控磁盘剩余空间及SQL Server错误日志(Power Shell)
需求描述 在我们的生产环境中,大部分情况下需要有自己的运维体制,包括自己健康状态的检测等.如果发生异常,需要提前预警的,通知形式一般为发邮件告知. 在所有的自检流程中最基础的一个就是磁盘剩余空间检测. ...
- SQL Server自动化运维系列 - 监控磁盘剩余空间及SQL Server错误日志(Power Shell)
需求描述 在我们的生产环境中,大部分情况下需要有自己的运维体制,包括自己健康状态的检测等.如果发生异常,需要提前预警的,通知形式一般为发邮件告知. 在所有的自检流程中最基础的一个就是磁盘剩余空间检测. ...
- Linux查看磁盘剩余空间
Linux查看磁盘剩余空间 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ df 文件系统 1K-blocks 已用 可用 已用% 挂载点 /dev/sda8 ...
- SQL Server 自动化运维系列 - 监控磁盘剩余空间及SQL Server错误日志(Power Shell)
需求描述 在我们的生产环境中,大部分情况下需要有自己的运维体制,包括自己健康状态的检测等.如果发生异常,需要提前预警的,通知形式一般为发邮件告知. 在所有的自检流程中最基础的一个就是磁盘剩余空间检测. ...
- /tmp/crontab.tDoyrp: 设备上没有空间 查看文件夹所在分区 磁盘剩余空间 15g的root-mail大文件
问题诊断: 文件夹所在磁盘已满 问题确认: 查看文件夹所在磁盘剩余空间,找出空间被消耗的文件(集) 查看文件夹所在磁盘空间的所属文件(暂未解决) [root@hadoop1 /]# df -Bg /t ...
随机推荐
- 2019牛客暑期多校训练营(第七场)E F H I
E Find the median 题意:每次往序列中增加连续的[l,r]的数,每加入一次就询问当前序列的中位数. 解法:此题没有要求在线,那么直接离线+线段树+二分就可以了.求出每个端点之后排序得到 ...
- Codeforces 1188B 式子转化
思路:看到(a + b)想到乘上(a - b)变成平方差展开(并没有想到2333), 两边同时乘上a - b, 最后式子转化成了a ^ 4 - ka = b ^ 4 - kb,剩下的就水到渠成了. 0 ...
- [几何]计算不规则多边形的面积、中心、重心(Android,转)
转自:[几何]计算不规则多边形的面积.中心.重心 最近项目用到:在不规则多边形的中心点加一个图标.(e.g: xx地区发生暴雪,暴雪区域是多边形,给多边形中心加一个暴雪的图标) 之前的设计是,计算不规 ...
- Nginx有哪些作用?
Nginx有哪些作用? http协议代理 搭建虚拟主机 服务的反向代理 在反向代理中配置集群的负载均衡 什么是正向代理? 正向代理,意思是一个位于客户端和原始服务器(origin server)之 ...
- CNN基础二:使用预训练网络提取图像特征
上一节中,我们采用了一个自定义的网络结构,从头开始训练猫狗大战分类器,最终在使用图像增强的方式下得到了82%的验证准确率.但是,想要将深度学习应用于小型图像数据集,通常不会贸然采用复杂网络并且从头开始 ...
- 字符串操作——C语言实现
代码如下: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <asse ...
- [CSP模拟测试43、44]题解
状态极差的两场.感觉现在自己的思维方式很是有问题. (但愿今天考试开始的一刻我不会看到H I J) A 考场上打了最短路+贪心,水了60. 然而正解其实比那30分贪心好想多了. 进行n次乘法后的结果一 ...
- zju1610Count the Colors
ZOJ Problem Set - 1610 Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting s ...
- MySQL-8.0填坑
Client does not support authentication protocol 或 Authentication plugin 'caching_sha2_password' cann ...
- SPSS转换菜单:创建时间序列
SPSS转换菜单:创建时间序列 1.概念:"创建时间序列"对话框允许您基于现有数值型时间序列变量的函数创建新的变量.这些转换后的值在时间序列分析中非常有用. 2.操作:转换-创建时 ...