?
Solved

PHP Text File Search

Posted on 2003-03-13
9
Medium Priority
?
218 Views
Last Modified: 2013-11-18
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
Comment
Question by:dean_dodd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 15

Expert Comment

by:VGR
ID: 8130985
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
 
LVL 4

Author Comment

by:dean_dodd
ID: 8131583
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
 
LVL 15

Expert Comment

by:VGR
ID: 8131749
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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 2

Expert Comment

by:ASCII_Man
ID: 8134782
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
 
LVL 4

Author Comment

by:dean_dodd
ID: 8134820
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
 
LVL 2

Accepted Solution

by:
ASCII_Man earned 600 total points
ID: 8136364
<?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
 
LVL 4

Author Comment

by:dean_dodd
ID: 8136603
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
 
LVL 2

Expert Comment

by:ASCII_Man
ID: 8151199
Change the second line from:
--------------------------
error_reporting(E_ALL);
--------------------------

to:
--------------------------
error_reporting(0);
--------------------------
0
 
LVL 4

Author Comment

by:dean_dodd
ID: 8151633
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question