Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

PHP Text File Search

I have no programming experience at all and qould like some help with this, I want to create a search engine via a web interface to search files on the local hard drive.  My plan is to create a txt file with all the files in and then use a php script to search.

I found this script with opens the txt file but I have no idea of how to get it to search the text file??

<?php

$fp = @fopen("ftp.txt", "rb") or die("Couldn't open file");
$data = fread($fp, filesize("ftp.txt"));

while(!feof($fp))
{
$data .= fgets($fp, 1024);
}

fclose($fp);

$values = explode("\r\n", $data);

echo "Results: " . "</p>";
echo "" . $values[0] . "<br>";
echo "" . $values[2] . "<br>";
echo "". $values[3];

?>


Thanks for any help ;)
0
dean_dodd
Asked:
dean_dodd
  • 4
  • 3
  • 2
1 Solution
 
VGRCommented:
easy.
$values[] contains the lines returned (without the CRLFs)

to search for a given $sewo word in that set of lines, do :
$notfound=TRUE;
$i=0;
while ($notfound) {
  $notfound=(strpos($values[$i],$sewo)===FALSE);
  $i++;
}
$i--; // to have the correct index of the first line containing the word


PS : personally, I would exec('GREP /S') (recursive directory search) or (if you are lucky enough to have DR-DOS) 'tree /S/R'
0
 
dean_doddAuthor Commented:
I using Windows XP!  I've done a Dir /A:D /B /S and out put the results to a txt file I dont mind having to create this file as I can schedule XP to do this once every night or something.

I have no idea how to encorporate your addon to my file I have tried pasting it near the bottom of the existing script but it does not work?

Sorry I have very little programming knowledge!

Thanks for your help though.

0
 
VGRCommented:
my little piece of code searches for the contents of variable $sewo (that you have to set up properly) in the $values[] array returned by your previous script.

it should work

but it doesn't display anything : YOU have to decide what to do when the word is found (or not)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ASCII_ManCommented:
Here's 10 minutes worth that should do everything you want, and explain it as well:

<?php
$searchString = 'boot';

// BEGIN SETTINGS
$treeFile = 'ftp.txt';
$caseSensitive = false;
$verbose = false;
// END SETTINGS

// BEGIN MAIN SCRIPT
$results = array();

echo (($verbose) ? "Reading directory tree from ".$treeFile."\r\n" : '');
$tree = file($treeFile);

echo (($verbose) ? "Counting number of directories in the tree\r\n" : '');
$numDirectories = count($tree);

echo (($verbose) ? "Found ".$numDirectories." directories, searching them now\r\n": '');
$searchLoops = $numDirectories - 1;
for($loopy = 0; $loopy < $searchLoops; $loopy++){
     if($caseSensitive){
          if(stristr($tree[$loopy], $searchString)){
               $results[] = $tree[$loopy];
               echo (($verbose) ? "HIT (".$tree[$loopy].")\r\n" : '');
          }
          else{
               echo (($verbose) ? "MISS (".$tree[$loopy].")\r\n" : '');
          }
     }
     else{
          if(stristr($tree[$loopy], $searchString)){
               $results[] = $tree[$loopy];
               echo (($verbose) ? "HIT (".$tree[$loopy].")\r\n" : '');
          }
          else{
               echo (($verbose) ? "MISS (".$tree[$loopy].")\r\n" : '');
          }
     }
}
echo (($verbose) ? "Search complete, results follow:\r\n": '');


echo "Results:\r\n";
foreach($results AS $thisResult){
     echo "\t".$thisResult;
}
// END MAIN SCRIPT
?>

Anything else you want it to do?
0
 
dean_doddAuthor Commented:
hi thanks ASCII_Man thats the kind of thing I want, if possible can it display a search box first to input the search and display the results on seperate lines?

Thanks :))
0
 
ASCII_ManCommented:
<?php
error_reporting(E_ALL);
?>
<html>
<head>
<title>FTP Search Script</title>
</head>
<body>
<form action="<?=$HTTP_SERVER_VARS['PHP_SELF']?>" method="post">
What are you searthing for?
<br>
<input name="searchString" value="<?=(($HTTP_POST_VARS['searchString'] != null) ? str_replace('"', '\\"', $HTTP_POST_VARS['searchString']) : '')?>" >
<input type="submit" value="submit">
<br>
<input name="verbose" type="checkbox" value="true"> Verbose mode
<br>
</form>
<?php
// BEGIN SETTINGS
$treeFile = 'ftp.txt';
$caseSensitive = false;
// END SETTINGS

// BEGIN MAIN SCRIPT
$verbose = (($HTTP_POST_VARS['verbose'] == "true") ? true : false);
if($HTTP_POST_VARS['searchString'] != null){
     $results = array();
     
     echo (($verbose) ? "Reading directory tree from ".$treeFile."\r\n<br>\r\n" : '');
     $tree = file($treeFile);
     
     echo (($verbose) ? "Counting number of directories in the tree\r\n<br>\r\n" : '');
     $numDirectories = count($tree);
     
     echo (($verbose) ? "Found ".$numDirectories." directories, searching them now\r\n<br>\r\n": '');
     $searchLoops = $numDirectories - 1;
     for($loopy = 0; $loopy < $searchLoops; $loopy++){
          if($caseSensitive){
                if(stristr($tree[$loopy], $HTTP_POST_VARS['searchString'])){
                      $results[] = $tree[$loopy];
                      echo (($verbose) ? "HIT (".$tree[$loopy].")\r\n<br>\r\n" : '');
                }
                else{
                      echo (($verbose) ? "MISS (".$tree[$loopy].")\r\n<br>\r\n" : '');
                }
          }
          else{
                if(stristr($tree[$loopy], $HTTP_POST_VARS['searchString'])){
                      $results[] = $tree[$loopy];
                      echo (($verbose) ? "HIT (".$tree[$loopy].")\r\n<br>\r\n" : '');
                }
                else{
                      echo (($verbose) ? "MISS (".$tree[$loopy].")\r\n<br>\r\n" : '');
                }
          }
     }
     echo (($verbose) ? "Search complete, results follow:\r\n<br>\r\n" : '');
     
     $loopy = 0;
     echo "<b>Results:</b>\r\n<br>\r\n";
     foreach($results AS $thisResult){
          echo '&nbsp;&nbsp;'.++$loopy.'. '.$thisResult."<br>\r\n";
     }
}
// END MAIN SCRIPT
?>
</body>
</html>
0
 
dean_doddAuthor Commented:
That does exactly what I want - Perfect!  Only one problem, I get a few error's on the pages although it does do the search and work.

On the first page where you enter the search in the search box this is already in I guess it should be blank?

<br /><b>Notice</b>:  Undefined index:  searchString in <b>c:\inetpub\wwwroot\dean1.php</b> on line <b>12</b><br />

I also get there two errors below the search box

Notice: Undefined index: verbose in c:\inetpub\wwwroot\dean1.php on line 25

Notice: Undefined index: searchString in c:\inetpub\wwwroot\dean1.php on line 26


Then on the results page I get

Notice: Undefined index: verbose in c:\inetpub\wwwroot\dean1.php on line 25


Other than these few error's its GREAT - Thanks for your time.

Dean.
0
 
ASCII_ManCommented:
Change the second line from:
--------------------------
error_reporting(E_ALL);
--------------------------

to:
--------------------------
error_reporting(0);
--------------------------
0
 
dean_doddAuthor Commented:
Thanks that works fine now.  I asked this question twice so if you reply to the other one here I will award you those points too?

http://www.experts-exchange.com/Web/Q_20545299.html

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now