Symfony Finder : How to sort files by size?

492 Views Asked by At

The Finder Symfony component is powerful, but unfortunately, you cannot sort founded files by size.

See below. I think it could be helpful, for me at least.

1

There are 1 best solutions below

0
On BEST ANSWER
<?php

$finder = new Finder();
$finder->files()
    ->in(__DIR__)
    ->sort(function (\SplFileInfo $a, \SplFileInfo $b) {
        return filesize($a->getRealpath()) < filesize($b->getRealpath());
    });

foreach ($finder as $file) {
    echo filesize($file->getRealpath()) . PHP_EOL;
}

That's it!