Posts Treesize - shows recursive disk usage in current directory
Post
Cancel

Treesize - shows recursive disk usage in current directory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
    $path = getcwd();

    function getDirectorySize($path) {
        $objects = new DirectoryIterator($path);
        $size = 0;
        foreach ( $objects as $object ) {
            if ( $object->isFile() ) {
                $size += $object->getSize();
            }
        }

        return $size;
    }

    $objects = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS),
        RecursiveIteratorIterator::SELF_FIRST,
        RecursiveIteratorIterator::CATCH_GET_CHILD
    );

    $dirlist = [];
    foreach ( $objects as $name => $object ) {
        if ( $object->isDir() ) {
            $dirlist[$object->getPathName()]['size'] = getDirectorySize($object->getPathName());
			$dirlist[$object->getPathName()]['count'] = getFileCount($object->getPathName());
        }
    }

	function getFileCount($path) {
		// src - https://stackoverflow.com/a/15778200
		$size = 0;
		$ignore = array('.','..','cgi-bin','.DS_Store');
		$files = scandir($path);
		foreach($files as $t) {
			if(in_array($t, $ignore)) continue;
			if (is_dir(rtrim($path, '/') . '/' . $t)) {
				$size += getFileCount(rtrim($path, '/') . '/' . $t);
			} else {
				$size++;
			}   
		}
		return $size;
	}

    arsort($dirlist);

?>
<!doctype html>

<div class="container-fluid">

# <?php echo htmlentities($path) ?>

  <table class="table table-sm table-hover table-striped">
    <thead>
    <tr>
      <th>Path</th>
      <th class="text-right">Size</th>
    </tr>
    </thead>
      <?php foreach ( $dirlist as $dir => $x ) { ?>
        <tr>
          <th class="text-monospace"><?php echo htmlentities($dir) ?></th>
          <td class="text-right small text-nowrap"><?php echo '('.$x['count'].') ' . number_format($x['size'] / 1024, 0, ',', '.') ?> KB</td>
        </tr>
      <?php } ?>
  </table>
</div>

src - https://github.com/pipiscrew/php-treesize

ref - https://github.com/tomsommer/php-treesize

origin - https://www.pipiscrew.com/?p=19472 treesize-shows-recursive-disk-usage-in-current-directory

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags