symfonysymfony-finder

Symfony Finder : How to sort files by size?


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.


Solution

  • <?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!