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.
<?php
/**
* powered by @cafewebmaster.com
* 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;
}
if($q){
$results = php_grep($q, $path);
}
echo <<<HRD
<pre >
<form method=post>
<input name=path size=100 value="$path" /> Path
<input name=q size=100 value="$q" /> Query
<input type=submit>
</form>
$results
</pre >
HRD;
?>
| Attachment | Size |
|---|---|
| Download this free php script (PHP-iGrep File Search) | 625 bytes |
Similar entries
- Upload multiple images with PHP
- How to recursively create md5 and sha1 sum of your files.
- Repair all mysql databases-tables with PHP
- Smart Multi-Uploader and Thumbnail Creator from GIF/ JPEG/ PNG with PHP
- Howto protect admin.php / login.php with htaccess password
- How to setup auto_prepend_file with safe mode and open basedir restriction
- Url manipulation with mod_rewrite and php-catcher for beginners
- Search only in .php files under linux
- .htaccess examples
- Howto install Volatility (RAM / Memory Forensic Framework) in Windows
- How to mount shared Folder in VirtualBox and Ubuntu Linux
- Web2 style secure & flexible free php contact form with easy setup
- Import big SQL Files under WAMP or LAMP ?
- A small login to add to top of online testing scripts
- Fix for MS Word 2007 appcrash when closing
- Web2 style sicheres & flexibles free PHP-Formular with easy setup
- Redirect a query to multiple search engines with one form and javascript
- XP like "Show Desktop Icon" for Windows7
- Check password strength / safety with PHP and Regex
- 'Similar entries' block cannot be saved












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