PHP; Search your files

Wouldn’t it be nice if you could search through your files with a webinterface? No need for access to the server, no need to know long and difficult commands, no need to have a special user who has access to do it, no…
I wrote a small script that allows you simply through a webbased to search in your files (directories)…
Maybe a small background story with this, all my backups are daily stored in folder in a date format. In my case this is YYYYMMDD. So you’ll find a lot of folder on my disks with 20111003, 20111105, 20111206, 20120101, 20120102… That’s the reason why i also added an option ‘search_date’. Normally i had a date picker for this, but i’ve removed it in this example since the core of this article is the search option… :wink:

The most important part of th config is following line

$command = “grep -cRnI” . ($case_insensitive ? “i” : “”) . " " . escapeshellarg($_POST[‘grep_pattern’]) . " " . $search_path . “” . $search_date ." ls | grep '^n..l..' | grep -v “.svn” | sort ";

Once you understand this line, you’ll able to do a lot of wonderful things with the page…
Look below for stuff in the body with $xxxxx. This should give you a pretty good idea what you need to change…

> php_grep.php
> <ul>
> <li><code>Node: <a href="php_cat.php?filepath= . htmlspecialchars($filepath) . &quot;&amp;lineno=" target="_blank" rel="noopener noreferrer"><span class="filename">" . htmlspecialchars($filepath) . "</span></a> --&gt; Results: " . $lineno . " </code>
> <pre><code><span class="match_component">" . htmlspecialchars($matched_line) . "</span></code></pre>
> </li>
> </ul>
> <p><code>"; } } ?&gt; </code></p>
> <p>&nbsp;</p>
> <p>&nbsp;</p>
> <h1><code><span style="text-decoration: underline;"><span style="color: red;">Search through the backups</span></span></code></h1>
> <form accept-charset="utf-8" method="post">
> <table>
> <tbody>
> <tr>
> <td><code><label for="grep_pattern">Search for: </label></code></td>
> <td><code><input name="grep_pattern" type="text" value="" id="grep_pattern" /></code></td>
> </tr>
> <tr>
> <td><code><label for="search_during">Search during: </label></code></td>
> <td><code>DateInput('search_date', true, 'YYYYMMDD') </code></td>
> </tr>
> <tr>
> <td><code><label for="search_path">Search in: </label></code></td>
> <td><code><select name="search_path" id="search_path"><option value="failed">Select here your desired folder</option><option value="backups/leafs_n/">Leaf N</option><option value="backups/leafs_dn/">Leafs DN</option><option value="backups/b2b/">B2B</option><option value="backups/b2c/">B2C</option><option value="backups/corporate/">Corporate</option><option value="backups/firewalls/">Firewalls</option><option value="backups/preprod/">PreProd</option><option value="backups/sb/">Service Bureau</option><option value="backups/sinsys/">SinSys</option><option value="backups/vpn/">VPN</option><option value="backups/wlp/">WLP</option><option value="backups">All (not archived) backups (Takes a while)</option></select></code></td>
> </tr>
> <tr>
> <td><code><label for="case_insensitive">Case Insensitive</label></code></td>
> <td><code><input checked="checked" name="case_insensitive" type="checkbox" value="1" id="case_insensitive" /></code></td>
> </tr>
> </tbody>
> </table>
> <p><code><input type="submit" value="Search! →" /></code></p>
> </form><hr />
> <h1><span style="color: red;">Results</span></h1>
> <hr />
> <h3><a name="sort1"><span style="color: green;"><b>1. Normal output</b></span></a></h3>
> <center>
> <ul>Start of the results for normal output</ul>
> </center>
> <p><br /> </p>
> <pre>&nbsp;</pre>
> <center><span style="text-decoration: underline;">End of the results for normal output</span></center>
> <p align="right"><a name="sort1"></a><a href="#top">Back to the top</a></p>
> <hr />
> <h3><a name="sort2"><span style="color: green;"><b>2. Numerical output</b></span></a></h3>
> <p>This gives you just a list with all files in numerical order and a number of results that was found.</p>
> <center><span style="text-decoration: underline;">Start of the results for numerical output</span></center>
> <pre>&nbsp;</pre>
> <center><span style="text-decoration: underline;">End of the results for numerical output</span></center>
> <p align="right"><a href="#top">Back to the top</a></p>
> <hr />
> <h3><a name="sort3"><span style="color: green;"><b>3. Results output</b></span></a></h3>
> <p>This gives you just a list with all files in alphabetical order and the return of the result line.</p>
> <center><span style="text-decoration: underline;">Start of the results for results output</span></center>
> <pre>&nbsp;</pre>
> <center><span style="text-decoration: underline;">End of the results for results output</span></center>
> <p align="right"><a href="#top">Back to the top</a></p>
> <hr />
> <h3><a name="debug"> <span style="color: green;">4. Debugging</span></a></h3>
> <p>This gives you some debug outputs for the 3 search outputs...</p>
> <ul>
> <li><code>Debugs for 'Normal output' Command ran: | Last line of result was: | Return code is: | Number of results: <code> </code></code></li>
> <li><code>Debugs for 'numerical output' Some results for debugging: Command ran: | Last line of result was: | Return code is: | Number of results: </code></li>
> <li>Debugs for 'result outputs' Some results for debugging: Command ran: | Last li ne of result was: | Return code is: | Number of results:</li>
> </ul>
> <p align="right"><a href="#top">Back to the top</a></p>
> <hr />