bux23
4/21/2017 - 1:52 PM

find string in file (search in directory and subdirectories) php

find string in file (search in directory and subdirectories) php

<?php

$string = 'stringToSearch';

$dir = new RecursiveDirectoryIterator('folder');
foreach (new RecursiveIteratorIterator($dir) as $filename => $file) {
    //$thename = $file->getFilename(); //uncomment this line and next to print names of all files scanned
    //echo $thename.'<br />';
    $content = file_get_contents($file->getPathname());
    if (strpos($content, $string) !== false) {
        echo "<br /><b>string found in file: ".$file->getPathname()."</b><br /><br />";
    }
}


?>