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分钟

相关推荐
无敌最俊朗@1 小时前
STL-vector面试剖析(面试复习4)
java·面试·职场和发展
PPPPickup1 小时前
easychat项目复盘---获取联系人列表,联系人详细,删除拉黑联系人
java·前端·javascript
LiamTuc1 小时前
Java构造函数
java·开发语言
长安er1 小时前
LeetCode 206/92/25 链表翻转问题-“盒子-标签-纸条模型”
java·数据结构·算法·leetcode·链表·链表翻转
菜鸟plus+2 小时前
N+1查询
java·服务器·数据库
我要添砖java2 小时前
《JAVAEE》网络编程-什么是网络?
java·网络·java-ee
CoderYanger2 小时前
动态规划算法-01背包问题:50.分割等和子集
java·算法·leetcode·动态规划·1024程序员节
2501_937189232 小时前
2025 优化版神马影视 8.8 源码系统|零基础部署
android·源码·开源软件·源代码管理·机顶盒
菜鸟233号3 小时前
力扣513 找树左下角的值 java实现
java·数据结构·算法·leetcode
Neoest4 小时前
【EasyExcel 填坑日记】“Syntax error on token )“: 一次编译错误在逃 Runtime 的灵异事件
java·eclipse·编辑器