搜索PHP目录中的所有文件

  • 本文关键字:文件 PHP 搜索 php
  • 更新时间 :
  • 英文 :


我试图搜索目录中的所有文件,而不是一个,但我不知道该用什么来搜索。

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>";
}
}

我想在第3行做这样的事情:

$file = 'db/*';

这样它就可以搜索test.txt和该目录中的其他txt文件。

我试过在谷歌上搜索,但没有发现任何有用的东西,有帮助吗?

函数名为glob,它接受一个必需的参数,该参数是路径,它可以是文件的模式,它返回与该模式匹配的所有文件的路径数组。换句话说,您可以将此函数与">dir/*.txt";它将返回dir文件夹中的所有文件,并且具有txt扩展名

最新更新