Searching Files With Laravel

How easy is it to search for files with laravel? Using Symfony’s Finder class it is really, really easy!

Say I wanted to recursively search a directory for all the files with fish in the name, over 100KB in size and had some text: ‘awesome’ inside of the file. That would be a tough one with just native php but never to fear, Fabien Potencier is here. Oh and I forgot to mention, I only want files that have been modified since yesterday. Oh and can you sort those by name for me? Thanks.

Since Laravel piggybacks off so many Symfony components, we can already use this Finder class in any Laravel application like so:

$finder = new Symfony\Component\Finder\Finder();

$finder->files()
    ->in($dir)
    ->size('< 100K')
    ->name("{$search}")
    ->date('since yesterday')
    ->contains('awesome');

foreach ($finder as $file)
{
	print $file->getRealpath();
}

Bye, bye RecursiveDirectoryIterator, may we never meet again! If I ever meet Fabien I’m likely to get a restraining order slapped on me after I give him a big hug and kiss.

post by K.D. on 09/09/2014