search text in files
by Guna[ Edit ] 2012-10-10 16:08:47
Think of a scenario where you need to remove a piece of code in your files that has a particular word in it. This can be easily done in linux terminal by issuing following command,
find . | xargs grep 'getTags' -sl
Above command finds the files which has 'getTags' in it, similar to the results given below:
./admin/preview.php
./common.php
./add-scraps.php
./classes/class.scrap.php
./myscraps.php
./events.php
./HRAS/login/users.php
Now it becomes easy to remove the code in those files only instead of searching all the files. But when your files hosted in shared server you wont get terminal access, what will you do?
You need to bring the same functionality through php code. So create a file called "
find.php" and paste the following code in it.
ob_start();
$q=$_GET['q'];
passthru("find . | xargs grep '$q' -s ");
$matches=ob_get_clean();
$matches=explode("\n",$matches);
echo "<h3>Search results for "$q"</h3>";
echo "<pre>";print_r($matches);echo "</pre>";
then access the file like this,
http://wesitename/find.php?q=getTags
This is for our internal use only and we need to protect it from being indexed in search engines, so add following code in htaccess:
<Files "find.php">
AuthName "Member Only"
AuthType Basic
AuthUserFile /home/username/.htpasswd
require valid-user
</Files>
Before placing this code you need to create a htpasswd file with a username and password