Search in text files recursively with PHP - Grep

Every linux user knows the power of grep, which saves us hours. Sometimes we need to find a string/word from html-output of a large CMS, but we dont know in which directoty/file is it. PHP-Grep helps us in this situation with searching in all directories/files under given path.


*	powered by
*	free for private use
*	please support us with donations

	define("SLASH", stristr($_SERVER[SERVER_SOFTWARE], "win") ? "\\" : "/");
	$path	= ($_POST[path]) ? $_POST[path] : dirname(__FILE__) ;
	$q		= $_POST[q];

	function php_grep($q, $path){
		$fp = opendir($path);
		while($f = readdir($fp)){
			if( preg_match("#^\.+$#", $f) ) continue; // ignore symbolic links
			$file_full_path = $path.SLASH.$f;
			if(is_dir($file_full_path)) {
				$ret .= php_grep($q, $file_full_path);
			} else if( stristr(file_get_contents($file_full_path), $q) ) {
				$ret .= "$file_full_path\n";
		return $ret;

		$results = php_grep($q, $path);

	echo <<



Download this free php script (PHP-iGrep File Search)625 bytes

And it works under both Linux and Microsoft Windows. Tested with Apache + PHP4/PHP5.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <pre> <h1> <h2> <h3> <h4> <h5> <h6> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <div> <style><img> <br> <blockquote>
  • Lines and paragraphs break automatically.
  • You may insert videos with [video:URL]

More information about formatting options

This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Enter the characters shown in the image. is a free online community for webdevelopers and beginners. Anybody can share their code, articles, tips, tutorials, code-examples or other webdesign related material on the site. Newbies can submit their questions and reply to existing questions. CW does not guarantee or warrant reliability of code, data and information published on the site. Use the site on your own risk. The site takes no responsibility of direct or indirect loss or any kind of harm to its users. The site also doesn't take responsibility of infected files or source code with any kind of infection or viruses, worms, spywares, malwares, trojan horses. CW reserves the right to edit, move, or delete any of content for any reason.