Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

Database Lookup PHP Script

Hello, I am currently making a database lookup tool that will let a user search for a term (username, name, etc.) and the engine will search mutliple text files for that specific term. Right now I am having an issue, I want the script to read all the files in the location given but it will only read one .txt file in that location. I want it to read multiple. if someone could help that would be awesome!


here is my PHP script for analysis:

Code
<?php
$file = glob('*.txt');
$query = $_GET['query'];

header('Content-Type: text/plain');

$contents = file_get_contents($file);

$pattern = preg_quote($query, '/');

$pattern = "/^.*$pattern.*\$/m";

if(preg_match_all($pattern, $contents, $matches)){
  echo "Found matches:\n";
  echo implode("\n", $matches[0]);
}
else{
  echo "No matches found";
}
That's because if you want to look more than one, you need to wrap it on a loop.

Try this:

PHP Code

<?php
$files 
= glob('*.txt'); //This returns an array of multiple files. You need to search it one by one.

foreach($files as $file) //This searches it one by one, until there are no more elements in the array.
{
    
$query = $_GET['query'];

    
header('Content-Type: text/plain');

    
$contents = file_get_contents($file);

    
$pattern = preg_quote($query, '/');

    
$pattern = "/^.*$pattern.*\$/m";

    if(
preg_match_all($pattern, $contents, $matches)){
     
 echo "Found matches:\n";
     
 echo implode("\n", $matches[0]);
    }
    else{
     
 echo "No matches found";
    }
} 
Read up on the foreach statement here: http://php.net/manual/en/control-structures.foreach.php
it works but it gives this error message when giving the results:

No matches found<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /storage/ssd4/176/496176/public_html/search/script.php:21) in <b>/storage/ssd4/176/496176/public_html/search/script.php</b> on line <b>8</b><br />
Found matches:
gonz0123
Move the

$query = $_GET['query'];

header('Content-Type: text/plain');

To outside of the foreach (on top of it, after the glob).
how would i go about displaying the name of the text file in the "found matches" portion?
I want it to say "found matches in (name of file.txt):"
(Nov 6, 2017 08:14 AM)Gonz0 Wrote:  how would i go about displaying the name of the text file in the "found matches" portion?
I want it to say "found matches in (name of file.txt):"

Just print your filename out?


PHP Code

<?php 
echo "Found matches in $file:\n"; 
thank you so much for your help!
(Nov 6, 2017 08:18 AM)Gonz0 Wrote:  thank you so much for your help!

No problem :)

You should read up on PHP at W3Schools, for starters (https://www.w3schools.com/php/)

And then move on up from there to the PHP Documentation itself (http://php.net/docs.php)

StackOverflow and Google are also very useful when you can't figure out how to complete certain tasks.

Best of Luck! ✌


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by Kasual™Oct 29, 2016 03:13 AM 24 3,711 Oct 29, 2016 03:13 AM
Last Post: Maudiy
[PHP] by Linus TorvaldsOct 8, 2016 11:09 AM 14 465 Oct 8, 2016 11:09 AM
Last Post: Linus Torvalds
[PHP] by ugofirstimtrustedMay 29, 2016 07:56 PM 8 213 May 29, 2016 07:56 PM
Last Post: ugofirstimtrusted