Alright. Done.
PHP Code:
<?php
function searchfiles($q,$dir) {
//Add the extensions you want to search through here.
$exts = array('.php','.html','.htm','.docx','.doc','.pdf');
//Enter the files that you DON'T want to search through, eg. this page.
$exceptions = array('somefilethatyoudontwant',basename($_SERVER['PHP_SELF']));
//Sets the beginning of the search results and outputs it.
echo '<h2>Your Search Results</h2><ol>';
flush();
//Opens the directory
$handle = opendir($dir);
//Starts a loop through the directory to list the files
while ($file = readdir($handle)) {
//Check if the file is an exception
if (in_array($q,$exceptions)) {
continue;
}
//Check if it's showing '.' meaning this directory (the parent directory) or the directory above it (..)
else if ($file == '.' || $file == '..') {
continue;
}
//Loops through all the extensions
foreach ($exts as $extension) {
//Checks if it's an allowed file extension (eg. Word Documents, HTML Files etc.)
if (strpos($file,$extension)) {
//Gets the file contents...
$c = file_get_contents($dir.'\\'.$file);
//See if there's a match...
if (strpos($c,$q)) {
//Adds the URL
echo '<li><a href="'.$dir.''.$file.'" title="'.$file.'">'.getTitle($c,$file).'</a><br>';
//Gets the body content
echo getBody($c);
//Ends the list item
echo'</li>';
//Outputs it. This allows for the item to be displayed while the page is still loading. Good for when handling large files.
flush();
}
}
//Or else it'll continue through the loop.
else {
continue;
}
}
}
//And close the list.
echo '</ol>';
}
//Tool: stripPHP($c)
//Strips the PHP stuff for you so you can't search it.
function stripPHP($c) {
if (strpos($c,'<?php') != FALSE) {
$php = '';
$php = explode('<?php',$c);
$php[1] = explode('?>',$php[1]);
$c = str_replace($php[1][0],'',$c);
$c = str_replace('<?php?>','',$c);
return $c;
}
else {
return $c;
}
}
//Tool: chopParagraph($str)
//Chops the long content.
function chopParagraph($str) {
$words = 20;
$chopped = '';
$str = explode(' ',$str);
if (count($str)>30) {
for ($i=0; $i <= $words; $i++) {
$chopped.=$str[$i].' ';
}
return $chopped.'...';
}
else {
return $str[count($str)-1];
}
}
//Tool: getBody($content)
//Gets the body content.
function getBody($content) {
$content = stripPHP($content);
if (strpos($content,'<body>')) {
$content = explode('<body>',$content);
$content[1] = explode('</body>',$content[1]);
$returner = strip_tags($content[1][0]);
}
else {
$returner = filterWeirdStuff($content); //For Word Documents...
}
return chopParagraph($returner);
}
//Tool: getTitle($content,$filename)
//Gets the title of the page. If there's no title, it'll display the page name.
function getTitle($content,$filename) {
$content = stripPHP($content);
if (strpos($content,'<title>')) {
$content = explode('<title>',$content);
$content[1] = explode('</title>',$content[1]);
return htmlspecialchars($content[1][0]);
}
else {
return $filename;
}
}
//Tool: filterWeirdStuff($str)
//Filters the ???????????????????***3%^#$^4 you see when trying to get the file contents of Word Documents. Ugh.
function filterWeirdStuff($str) {
$str = explode(' ',$str);
unset($str[0]);
unset($str[1]);
$str = implode(' ',$str);
return '...'.$str;
}
if (isset($_POST['Submit'],$_POST['term'],$_GET['search'])) {
searchfiles($_POST['term'], './');
}
else {
?>
<form action="?search=true" method="post">
<p>
<label>Search: <input type="text" name="term"></label>
</p>
<p>
<label><input type="submit" name="Submit" value="Search"></label>
</p>
</form>
<?php
}
?>
Try check and see if it works for Acrobat Documents. I've got a tonne of homework to do, so I couldn't check it right now.
Bookmarks