QQ截图20251009120159.png

<?php
// 获取磁盘使用情况(使用允许的目录路径)
function getDiskUsage() {
    // 使用当前脚本所在目录作为基准路径
    $baseDir = __DIR__;
     
    $total = disk_total_space($baseDir);
    $free = disk_free_space($baseDir);
    $used = $total - $free;
     
    // 防止除以零错误
    if ($total == 0) {
        return [
            'total' => '未知',
            'free' => '未知',
            'used' => '未知',
            'percent' => 0,
            'percentFree' => 0
        ];
    }
     
    $percent = round(($used / $total) * 100, 2);
    $percentFree = round(($free / $total) * 100, 2);
     
    return [
        'total' => formatBytes($total),
        'free' => formatBytes($free),
        'used' => formatBytes($used),
        'percent' => $percent,
        'percentFree' => $percentFree
    ];
}
 
// 格式化字节大小
function formatBytes($bytes, $precision = 2) {
    if ($bytes == 0) return '0 B';
     
    $units = ['B', 'KB', 'MB', 'GB', 'TB'];
    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);
     
    $bytes /= pow(1024, $pow);
     
    return round($bytes, $precision) . ' ' . $units[$pow];
}
 
// 获取系统信息
function getSystemInfo() {
    return [
        'os' => php_uname('s') . ' ' . php_uname('r'),
        'php_version' => PHP_VERSION,
        'server_software' => $_SERVER['SERVER_SOFTWARE'] ?? '未知',
        'server_time' => date('Y-m-d H:i:s')
    ];
}
 
// 获取磁盘使用情况
$diskUsage = getDiskUsage();
$systemInfo = getSystemInfo();
?>
 
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>磁盘使用统计</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        body {
            background-color: #f8f9fa;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        .dashboard {
            max-width: 1200px;
            margin: 30px auto;
            padding: 20px;
        }
        .card {
            border-radius: 10px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
            margin-bottom: 20px;
            border: none;
        }
        .card-header {
            background-color: #4a6fdc;
            color: white;
            font-weight: 600;
            border-radius: 10px 10px 0 0 !important;
            padding: 12px 20px;
        }
        .progress {
            height: 25px;
            border-radius: 15px;
            background-color: #e9ecef;
        }
        .progress-bar {
            border-radius: 15px;
            font-weight: 600;
        }
        .stat-card {
            text-align: center;
            padding: 20px;
            border-radius: 10px;
            background-color: white;
            box-shadow: 0 2px 8px rgba(0,0,0,0.08);
            margin-bottom: 20px;
            transition: transform 0.3s;
        }
        .stat-card:hover {
            transform: translateY(-5px);
        }
        .stat-value {
            font-size: 2.2rem;
            font-weight: 700;
            margin: 10px 0;
        }
        .stat-label {
            color: #6c757d;
            font-size: 0.9rem;
        }
        .chart-container {
            position: relative;
            height: 300px;
            width: 100%;
        }
        .system-info {
            background-color: #f8f9fa;
            border-radius: 8px;
            padding: 15px;
        }
        .system-info-item {
            margin-bottom: 10px;
            display: flex;
            justify-content: space-between;
        }
        .system-info-label {
            font-weight: 600;
            color: #495057;
        }
        .system-info-value {
            color: #6c757d;
        }
    </style>
</head>
<body>
    <div class="dashboard">
        <div class="row mb-4">
            <div class="col-12">
                <h1 class="text-center">磁盘使用统计</h1>
                <p class="text-center text-muted">当前目录: <?php echo __DIR__; ?></p>
            </div>
        </div>
         
        <div class="row mb-4">
            <div class="col-md-4">
                <div class="stat-card">
                    <div class="stat-label">总空间</div>
                    <div class="stat-value text-primary"><?php echo $diskUsage['total']; ?></div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="stat-card">
                    <div class="stat-label">已用空间</div>
                    <div class="stat-value text-danger"><?php echo $diskUsage['used']; ?></div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="stat-card">
                    <div class="stat-label">可用空间</div>
                    <div class="stat-value text-success"><?php echo $diskUsage['free']; ?></div>
                </div>
            </div>
        </div>
         
        <div class="row mb-4">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">磁盘使用率</div>
                    <div class="card-body">
                        <div class="progress mb-3">
                            <div class="progress-bar bg-danger" role="progressbar"
                                style="width: <?php echo $diskUsage['percent']; ?>%"
                                aria-valuenow="<?php echo $diskUsage['percent']; ?>"
                                aria-valuemin="0" aria-valuemax="100">
                                <?php echo $diskUsage['percent']; ?>%
                            </div>
                        </div>
                        <div class="d-flex justify-content-between">
                            <span>已用: <?php echo $diskUsage['percent']; ?>%</span>
                            <span>可用: <?php echo $diskUsage['percentFree']; ?>%</span>
                        </div>
                    </div>
                </div>
            </div>
             
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">磁盘使用分布</div>
                    <div class="card-body">
                        <div class="chart-container">
                            <canvas id="diskChart"></canvas>
                        </div>
                    </div>
                </div>
            </div>
        </div>
         
        <div class="row">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">系统信息</div>
                    <div class="card-body">
                        <div class="system-info">
                            <div class="system-info-item">
                                <span class="system-info-label">操作系统:</span>
                                <span class="system-info-value"><?php echo $systemInfo['os']; ?></span>
                            </div>
                            <div class="system-info-item">
                                <span class="system-info-label">PHP版本:</span>
                                <span class="system-info-value"><?php echo $systemInfo['php_version']; ?></span>
                            </div>
                            <div class="system-info-item">
                                <span class="system-info-label">服务器软件:</span>
                                <span class="system-info-value"><?php echo $systemInfo['server_software']; ?></span>
                            </div>
                            <div class="system-info-item">
                                <span class="system-info-label">服务器时间:</span>
                                <span class="system-info-value"><?php echo $systemInfo['server_time']; ?></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
             
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">磁盘空间详情</div>
                    <div class="card-body">
                        <table class="table table-striped">
                            <tbody>
                                <tr>
                                    <td>总空间</td>
                                    <td class="text-end"><?php echo $diskUsage['total']; ?></td>
                                </tr>
                                <tr>
                                    <td>已用空间</td>
                                    <td class="text-end text-danger"><?php echo $diskUsage['used']; ?></td>
                                </tr>
                                <tr>
                                    <td>可用空间</td>
                                    <td class="text-end text-success"><?php echo $diskUsage['free']; ?></td>
                                </tr>
                                <tr>
                                    <td>使用率</td>
                                    <td class="text-end"><?php echo $diskUsage['percent']; ?>%</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
 
    <script>
        // 磁盘使用饼图
        const ctx = document.getElementById('diskChart').getContext('2d');
        const diskChart = new Chart(ctx, {
            type: 'pie',
            data: {
                labels: ['已用空间', '可用空间'],
                datasets: [{
                    data: [<?php echo $diskUsage['percent']; ?>, <?php echo $diskUsage['percentFree']; ?>],
                    backgroundColor: [
                        '#dc3545',
                        '#28a745'
                    ],
                    borderWidth: 0
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    legend: {
                        position: 'bottom',
                        labels: {
                            font: {
                                size: 14
                            },
                            padding: 20
                        }
                    },
                    tooltip: {
                        callbacks: {
                            label: function(context) {
                                return context.label + ': ' + context.raw + '%';
                            }
                        }
                    }
                }
            }
        });
    </script>
</body>
</html>

转自吾爱破解

最后修改:2025 年 10 月 09 日
如果觉得我的文章对你有用,请随意赞赏