this content was originally posted here.

search within files in a directory

During my quest to create a semi-static site that is searchable and fast to load.

I thought while loading all of this content in with ajax and such that it would be interesting if i could do a text search within files using just php and no databases.

Here is two ways to check a single file's content using a string match

if( strpos(file_get_contents("./file.txt"),$_GET['id']) !== false) {
    // do stuff
}

or this one which uses a cmd shell grep to find the file, which i prefer not to use but will see how performance goes.

if( exec('grep '.escapeshellarg($_GET['id']).' ./file.txt')) {
	// do stuff
}

so now we have to add this to a loop through each file in a directory

header('Content-Type: application/json');

$dir = "./html/";
$file_type = ".html";
$dir_array[] = "";
$searchval = "php";

// Open a directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false) {
      	if (strpos($file, $file_type) !== false) {
      		if($file > " ") {
      			$file_nice = $file;
      			$file_nice = str_replace("-", " ", $file_nice);
      			$file_nice = str_replace(".html", "", $file_nice);
      			// $file = str_replace(".html", "", $file);

            if( strpos(file_get_contents($dir.$file),$searchval) !== false) {
                $dir_array[$file] = $file_nice;
            }

		    	
			}
		}
    }
    closedir($dh);
  }
}
asort($dir_array);
echo json_encode(array_filter($dir_array));	

this is untested code, but i think this should do it. it should add the file name to the array if the $searchval is located

Sources

Source

Demo

database file: /home/kruxor/Dropbox/ssl.kruxor.com/root/kruxor.com/db/db.db
comments_dbfile: /home/kruxor/Dropbox/ssl.kruxor.com/root/kruxor.com/db/comments.db ($db_comments)
views_dbfile: /home/kruxor/Dropbox/ssl.kruxor.com/root/kruxor.com/db/views.db
$dbfood_file: /home/kruxor/Dropbox/ssl.kruxor.com/root/kruxor.com/db/food.db
db_linx: /home/kruxor/Dropbox/ssl.kruxor.com/root/kruxor.com/linx/db/db.db
Classes Loaded:
class/answers.class.php
class/code.class.php
class/comments.class.php
class/content.class.php
class/core.class.php
class/crud.class.php
class/crumbs.class.php
class/featured.class.php
class/food.class.php
class/functions.class.php
class/images.class.php
class/import.class.php
class/links.class.php
class/questions.class.php
class/template.class.php
class/tv.class.php
class/user.class.php
class/utilities.class.php
class/views.class.php
class/widget.class.php
Class Extend Loaded: class/extends/expenses.extend.php
Class Extend Loaded: class/extends/gary_cummins_timeline.extend.php
Class Extend Loaded: class/extends/moo.extend.php
Class Extend Loaded: class/extends/osrs_item_data.extend.php
Class Extend Loaded: class/extends/osrs_slayer.extend.php
Class Extend Loaded: class/extends/osrs_slayer_monsters.extend.php
Class Extend Loaded: class/extends/searches.extend.php
Core Database Connected: expenses.db
expenses add_to_menu:
Class Extend Auto Initialized as: expenses
Core Database Connected: gary_cummins_timeline.db
gary_cummins_timeline add_to_menu:
Class Extend Auto Initialized as: gary_cummins_timeline
Core Database Connected: moo.db
moo add_to_menu:
Class Extend Auto Initialized as: moo
Core Database Connected: osrs_item_data.db
osrs_item_data add_to_menu:
Class Extend Auto Initialized as: osrs_item_data
Core Database Connected: osrs_slayer.db
osrs_slayer add_to_menu:
Class Extend Auto Initialized as: osrs_slayer
Core Database Connected: osrs_slayer_monsters.db
osrs_slayer_monsters add_to_menu:
Class Extend Auto Initialized as: osrs_slayer_monsters
Core Database Connected: searches.db
searches add_to_menu:
Class Extend Auto Initialized as: searches
$user_logged_in:
user access level:
pages:
$p1:view
$p2:code
$p3:Sw4Ia
$p4:search-within-files-in-a-directory
$p5:
$p6:
$p:/view/code/Sw4Ia/search-within-files-in-a-directory/
Using Template: view-code.html