Searching for a text string in all files and directories recursively in PHP

Sometimes you need to edit some part of your website and unfortunately it isn’t made by you. You will, obviously, need to search for the file in which that particular part of site exists. In CMSs/forums/blogs, it get really hard to search it manually.

I was having a similar problem. I needed to edit the footer of my site http://www.techmindz.com but I didn’t know which file, out of 100s, had that particular part. I just made a script for that.

<?php

$query=escapeshellarg($_GET['str']);

$query="find . -exec grep -l $query {} ;";

system($query);

?>

Save this file as textsearch.php in your root directory. Now visit the site www.yoursite.com/textsearch.php?str=yoursearchstring

And voila! You get the results.

Peace!

DisclaimerPlease note that this should be used only by webmasters as a quick tool to find specific strings within all files without having to download all the files to local computer. While this method is not recommended, this can be used as a temporary tool only. This file (textsearch.php) should be deleted as soon as your work is done with it for security reasons. I won’t be held responsible for any damages caused.

Leave a Reply

Your email address will not be published. Required fields are marked *