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

文章描述了一个名为FindTopSize.php的PHP类,用于在给定目录下查找并按大小对文件和子目录进行排序,支持file和dir两种处理方式。脚本演示了如何统计C盘目录中30万文件的大小,执行时间大约5分钟。
摘要由CSDN通过智能技术生成
FindTopSize.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
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分钟

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

etafort

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值