I'm trying to search all of the files in a directory instead of one, but I don't know what to use to do that.
results.php:
<?php
// error_reporting(E_ERROR | E_PARSE);
$file = 'db/test.txt';
$searchfor = $_GET['q'];
if (!$_GET['q']) { // returns this if query is empty
echo "Search something.";
}
else {
// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
if (preg_match_all($pattern, $contents, $matches))
{
echo "<centerResults</center><br>";
echo "<pre>";
echo implode($matches[0]);
echo "</pre>";
}
else
{
echo "<center>No results</center>";
}
}
I wanted to do something like this on line 3:
$file = 'db/*';
So that it could search through test.txt, and other txt files in that directory.
And i've tried googling, and nothing helpful has come up, any help?
CodePudding user response:
Ther is function called glob it accept a required parameter which is the path and it can be a pattern of the files and it return an array of the paths of all the files that match that pattern . in other words , you can use this function with "dir/*.txt" and it is going to return all the files that ar in the dir folder and has the txt extension