This class can be used to search for files in a given text directory.
It can be given a directory traversal recursively find files for certain file extensions.
And open the files found and check to see if they contain search terms.
It returns a list of all files containing an array of search words.
Copy Code code as follows:
<?php
/*
Class for searching the contents of all the files in a directory and its subdirectories
For support Visit http://www.webdigity.com/
*/
Class searchfilecontents{
var $dir _name = ';//the directory to search
var $search _phrase = ';//the phrase to search in the file contents
var $allowed _file_types = Array (' php ', ' Phps ');//the file types that are searched
var $foundFiles//files that contain the search phrase is stored here
Open Source Code OSPHP.COM.Cn
var $myfiles;
function Search ($directory, $search _phrase) {
$this->dir_name = $directory;
$this->search_phrase = $search _phrase;
$this->myfiles = $this->getdircontents ($this->dir_name);
$this->foundfiles = Array ();
if (Empty ($this->search_phrase)) die (' empty search phrase ');
if (Empty ($this->dir_name)) Die (' You must select a directory to search ');
foreach ($this->myfiles as $f) {
if (In_array array_pop (Explode ('. ', $f)), $this->allowed_file_types)) {//Open source OSPhP.COM.CN
$contents = file_get_contents ($f);
if (Strpos ($contents, $this->search_phrase)!== false)
$this->foundfiles [] = $f;
Open Source Code OSPhP.COm.CN
}
}
return $this->foundfiles;
}
function Getdircontents ($dir) {
if (!is_dir ($dir)) {die ("Function getdircontents:problem reading: $dir!");}
if ($root = @opendir ($dir)) {
PHP Open Source Code
while ($file =readdir ($root)) {
if ($file = = "." | | $file = = "...") {continue;}
if (Is_dir ($dir.) /". $file)) {
$files =array_merge ($files, $this->getdircontents ($dir.) /". $file));
}else{
$files []= $dir.] /". $file; Open Source OSPhP.COM.CN
}
}
}
return $files;
}
}
Example:
$search = new Searchfilecontents;
$search->search (' E:/htdocs/accessclass ', ' class '); Open Source Code OSPHP.COM.Cn
Var_dump ($search->foundfiles);
?>