/*
Author : evergreenphp
Website : http://evergreenphp.blogspot.com
Website Info : Lots of source code
FREEly available, in PHP, javascript, AJAX,
MySQL etc.,
Description : This program is used to search a word in entire files in a folder
not only that it will search in all the inner folder file.
*/
/* Configuration settings start */
$path_file = '/www/htdocs/evergreenphp';
/* Configuration settings ends */
$file_folder_list = scan_Dir($path_file);
$Files_list_array = Organize_files($file_folder_list);
$search_word_is = $_POST['search_word'];
/* ---------------------------------------------------------------------------- */
/* List out all files and folders in the Current Directory (Specific Directory) */
function scan_Dir($dir=".") {
$arrfiles = array();
if (is_dir($dir)) {
if ($handle = opendir($dir)) {
chdir($dir);
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($file)) {
//$arrfiles[] = $dir."/".$file;
$arr = scan_Dir($file);
$arrfiles[] = $dir."/".$file;
foreach ($arr as $value) {
$arrfiles[] = $dir."/".$value;
}
} else {
$arrfiles[] = $dir."/".$file;
}
}
}
chdir("../");
}
closedir($handle);
}
return $arrfiles;
}
/* Organize the files list */
function Organize_files($files_folders)
{
$Files_array = array();
for ($i=0; $i< count($files_folders); $i++)
{
$check = explode(".", $files_folders[$i]);
if (count($check) > 1) $Files_array[] = $files_folders[$i];
}
return $Files_array;
}
function Start_search($directory_array, $search_word){
echo '< table border="0" width="100%" cellspacing="2" cellpadding="1" align="center">' . "\n";
echo '< tr class="infoBoxContent">< td class="dataTableHeadingContent">' . ' Searching '.sizeof($directory_array).' files ... for: '.$search_word.'< /td>< /tr>' . "\n\n";
echo '< tr>< td> < /td>< /tr>';
// check all files located
$file_cnt = 0;
$cnt_found=0;
for ($i = 0, $n = sizeof($directory_array); $i < $n; $i++) {
// build file content of matching lines
$file_cnt++;
$file = $directory_array[$i];
//echo 'I SEE ' . $directory_array[$i] . '
';
// clean path name
while (strstr($file, '//')) $file = str_replace('//', '/', $file);
$show_file = '';
if (file_exists($file)) {
$show_file .= "\n" . '< tr>< td class="main">' . "\n";
$show_file .= '< tr class="infoBoxContent">< td class="dataTableHeadingContent">';
$show_file .= '' . $file . '';
$show_file .= '< /td>< /tr>';
$show_file .= '< tr>< td class="main">';
// put file into an array to be scanned
$lines = file($file);
$found_line = 'false';
// loop through the array, show line and line numbers
foreach ($lines as $line_num => $line) {
$cnt_lines++;
if (strstr(strtoupper($line), strtoupper($search_word))) {
$found_line= 'true';
$found = 'true';
$cnt_found++;
$show_file .= "< br />Line #{$line_num} : " ;
$show_file .= htmlspecialchars($line);
$show_file .= "< br />\n";
} else {
if ($cnt_lines >= 5) {
$cnt_lines=0;
}
}
}
}
$show_file .= '< /td>< /tr>' . "\n";
// if there was a match, show lines
if ($found_line == 'true') {
echo $show_file ;
} // show file
}
echo '< tr>
}
?>
< html>
< body>
< form name="search_form" method="post" action="">
< table border="1">
< tr>
< td>Search Path:< /td>
< td>< /td>
< /tr>
< tr>
< td>Word to Search:< /td>
< td>< input type="text" name="search_word">
< /tr>
< tr>
< td colspan="2">< input type="Submit" name="search_submit" value="Start_search">< /td>
< /tr>
< tr>
< td colspan="2" align="left">
if ($_POST['search_word'] != ""){
Start_search($Files_list_array, $search_word_is);
}
?>
< /td>
< /tr>
< /table>
< /form>
< /body>
< /html>
No comments:
Post a Comment