好吧,我在一个名为Files的主目录下的多个目录中有几百个不同的文件.我如何在根目录(FIles)中创建一个PHP文件,然后搜索所有子目录等,然后将结果打印给用户?但是,我只希望这个搜索引擎搜索文件名而不是它的内容.如果可能,您可以提供已制作的脚本或示例代码吗?非常感谢!
要在PHP中搜索文件,您可以使用glob:
http://php.net/manual/en/function.glob.php
要获取路径的文件列表,您可以使用scandir:
http://php.net/manual/en/function.scandir.php
例
$dir = "/path/to/search/in/*.txt"; //search a path for only .txt files
foreach(glob($dir) as $file)
{
//print out the files that match
echo "filename: $file : filetype: " . filetype($file) . "<br />";
}
Run Code Online (Sandbox Code Playgroud)
自定义递归全局
来自:http://snipplr.com/view.php?codeview&id = 16233
rglob($pattern, $flags = 0, $path = '')
{
if (!$path && ($dir = dirname($pattern)) != '.')
{
if ($dir == '\\' || $dir == '/') $dir = '';
return rglob(basename($pattern), $flags, $dir . '/');
}
$paths = glob($path . '*', GLOB_ONLYDIR | GLOB_NOSORT);
$files = glob($path . $pattern, $flags);
foreach ($paths as $p) $files = array_merge($files, rglob($pattern, $flags, $p . '/'));
return $files;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1505 次 |
| 最近记录: |