php cli查找最大文件及文件夹占用排行2.0

复制代码
FindTopSize.php
php 复制代码
<?php
class FindTopSize
{
    public static $out_process = false;
    public static function findTop($directory, $handle = 'file', $limit = 10)
    {

        switch ($handle) {
            case 'file':
                $files = [];
                self::listFilesWithSize($files, $directory);
                $s = self::findTopFileSort($files, $limit);
                break;
            case 'dir':
                $dirs = [];
                self::listDirWithSize($dirs, $directory);
                $s = self::findTopDirSort($dirs, $limit);
                break;
            default:
                $s = [];
                break;
        }
        self::out($s);
    }

    private static function findTopDirSort($arr, $limit)
    {
        array_multisort(array_column($arr, 'len'), SORT_DESC, $arr);
        $pk = end($arr)['key'];
        foreach ($arr as $key => $item) {
            if(self::$out_process===true){
                echo $key."\n";
            }
            if($key===$pk)
                continue;
            $arr[sha1(substr($item['handle'],0,strrpos($item['handle'],'/')))]['size'] += $arr[$key]['size'];
        }
        array_multisort(array_column($arr, 'size'), SORT_DESC, $arr);
        $p = 1;
        foreach ($arr as $item) {
            if ($p > $limit) {
                break;
            }
            $p++;
            $res[] = $item;
        }
        return $res;
    }

    private static function findTopFileSort($arr, $limit)
    {
        array_multisort(array_column($arr, 'size'), SORT_DESC, $arr);
        $res = [];
        $p = 1;
        foreach ($arr as $item) {
            if ($p > $limit) {
                break;
            }
            $p++;
            $res[] = $item;
        }
        return $res;
    }

    private static function listFilesWithSize(&$files, $dir)
    {
        $handles = array_merge(glob($dir . '/.*'), glob($dir . '/*'));
        unset($handles[0]);
        unset($handles[1]);
        foreach ($handles as $handle) {
            if(self::$out_process===true){
                echo $handle . "\n";
            }
            if (is_dir($handle)) {
                self::listFilesWithSize($files, $handle);
            }
            if (is_file($handle)) {
                $files[] = [
                    'handle' => $handle,
                    'size' => filesize($handle),
                ];
            }
        }
    }

    private static function listDirWithSize(&$dirs, $dir)
    {
        if (count($dirs) === 0) {
            $dirs[sha1($dir)] = [
                'key' => sha1($dir),
                'pkey' => '',
                'handle' => $dir,
                'size' => 0,
                'len' => strlen($dir),
            ];
        }
        $handles = array_merge(glob($dir . '/.*'), glob($dir . '/*'));
        unset($handles[0]);
        unset($handles[1]);
        foreach ($handles as $handle) {
            if(self::$out_process===true){
                echo $handle . "\n";
            }
            if (is_dir($handle)) {
                $dirs[sha1($handle)] = [
                    'key' => sha1($handle),
                    'pkey' => sha1($dir),
                    'handle' => $handle,
                    'size' => 0,
                    'len' => strlen($handle),
                ];
                self::listDirWithSize($dirs, $handle);
            }
            if (is_file($handle)) {
                if (!empty($dirs[sha1($dir)]))
                    $dirs[sha1($dir)]['size'] += filesize($handle);
            }
        }
    }

    private static function out($arr)
    {
        echo "\n\n";
        foreach ($arr as $item) {
            echo self::convertSize($item['size']) . ' -> ' . $item['handle'] . "\n";
        }
    }

    private static function convertSize($size)
    {
        $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB',];
        for ($i = 0; $size >= 1024 && $i < count($units) - 1; $i++) {
            $size /= 1024;
        }
        return sprintf("%10s", sprintf("%.3f", round($size, 3)) . ' ' . $units[$i]);
    }

}

运行

run.php

php 复制代码
<?php
ini_set('memory_limit', '-1');
include_once 'D:/code/FindTopSize.php';

$time_start = time();

$directory = 'C:';
FindTopSize::$out_process = true;
FindTopSize::findTop($directory,'file',10);
//FindTopSize::findTop($directory, 'dir', 20);

$time_end = time();

// 计算执行时间
$time = $time_end - $time_start;
$time_s = secondsToTime($time);

echo "\n程序执行时间:".$time_s." \n";


function secondsToTime($seconds) {
    $hours = floor($seconds / 3600);
    $minutes = floor(($seconds % 3600) / 60);
    $seconds = $seconds % 60;
    $s = $hours.'时'.$minutes.'分'.$seconds.'秒';
    return $s;
}

执行

php -f run.php

开启cli模式开启jit效果更佳,扫描C盘【30万文件】目录大小耗时5分钟

相关推荐
SL_staff30 分钟前
10人团队如何用轻量私有化文档平台实现知识复用提效?技术视角拆解B/S架构下的协作闭环
java·开源·全栈
释厄62343 分钟前
同性线套娃律——圆面积必然=1+2+...+n 的求和·圆心不与 π 相关
java·人工智能·算法·1024程序员节
猫猫.1531 小时前
IO、存储、硬盘、文件系统详细总结
java·java-ee
SL_staff1 小时前
设备离线时业务不中断:JVS-IOT高可用架构解析
java·物联网·产品
郭小李2 小时前
mybatis 动态 sql
java·sql·mybatis
代龙涛2 小时前
WordPress Query 查询机制详解(WP_Query)
开发语言·后端·php·wordpress
瑞码空间2 小时前
JSP与Servlet核心开发与CRUD实战笔记
java·笔记·servlet·jsp
xcLeigh2 小时前
KingbaseES数据库教程:初识数据库,从安装到跑通第一条SQL
android·数据库·sql·安装·数据库教程·kes
知守观2 小时前
技术考古:iBATIS 2.3.4 时代的持久层,是如何用 Map 玩转 307 个存储过程的?
java·后端·mybatis
captain3763 小时前
MyBatis Plus(1)
java·服务器·mybatis