Search code examples
phpsymfonysearchsymfony-componentssymfony-finder

Symfony2 - Finder Component


I want to find specific texts in some files in a directory. I tried using Symfony Finder Component, but couldn't get the result. Here is my code below.

$finder = new Finder();
$finder->in('./sample')->files()->contains('This is test');

The files are in sample directory. There are pdf, txt files in sample directory. The above didn't work for me. How to get this to work. Any help will be appreciated.


Solution

  • Before this check the path of your sample directory if is in the same directory of your .php file this would work.

    $finder->files()->in(__DIR__.'/sample')->contains('This is test');
    var_dump($finder);
    

    DIR is magic constant of php that contains the directory of the file.