我如何在directoy中搜索文件名而不是PHP中的内容?
好的,我有几百个不同的文件在一个主目录下的多个目录下,叫做文件。如何在根目录文件中创建PHP文件,搜索所有子目录等,然后将结果打印给用户?但是,我只希望这个搜索引擎搜索文件名,而不是它的内容。如果可能,您可以提供一个已经制作好的脚本或示例代码吗?非常感谢 要在PHP中搜索文件,可以使用glob: 要获取路径的文件列表,可以使用scandir: 范例我如何在directoy中搜索文件名而不是PHP中的内容?,php,search,Php,Search,好的,我有几百个不同的文件在一个主目录下的多个目录下,叫做文件。如何在根目录文件中创建PHP文件,搜索所有子目录等,然后将结果打印给用户?但是,我只希望这个搜索引擎搜索文件名,而不是它的内容。如果可能,您可以提供一个已经制作好的脚本或示例代码吗?非常感谢 要在PHP中搜索文件,可以使用glob: 要获取路径的文件列表,可以使用scandir: 范例 要在PHP中搜索文件,可以使用glob: 要获取路径的文件列表,可以使用scandir: 范例 PHP5: PHP5: 原始项目: 原始项目:查看注
要在PHP中搜索文件,可以使用glob: 要获取路径的文件列表,可以使用scandir: 范例 PHP5: PHP5: 原始项目:
原始项目:查看注释部分,了解使用此功能的一些示例。您能提供一些最适合我的示例代码吗?这不会像问题中所问的那样搜索子目录。@salathe:True。我添加了一个递归版本。有关使用此函数的一些示例,请查看注释部分。您能否提供一些最适合我的示例代码?这不会像问题中所问的那样搜索子目录。@salathe:True。我添加了一个递归版本。当你说搜索文件名时,你想在文件目录和子目录中的任何位置列出具有该文件名的文件吗?是否需要部分文件名匹配,例如img匹配a_img1.gif等,还是仅匹配确切的名称?是否要在第一次匹配时停止,或列出所有匹配项?如果此问题不再需要其他答案,请将相应的帖子标记为答案。当您说搜索文件名时,是否要在文件目录和子目录中的任何位置列出具有该文件名的文件?是否需要部分文件名匹配,例如img匹配a_img1.gif等,还是仅匹配确切的名称?您想在第一场比赛时停止,还是列出所有比赛?如果此问题不再需要其他答案,请将相应的帖子标记为答案。谢谢!费萨尔,我能问一下这是什么许可证吗?你可以免费使用。谢谢!费萨尔,我能问一下这是什么许可证吗?你可以免费使用。
$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 />";
}
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;
}
$dir_iterator = new RecursiveDirectoryIterator("/path");
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
// could use CHILD_FIRST if you so wish
foreach ($iterator as $file) {
echo $file, "\n";
}
<?php
/***********************************************************************
* @name AnyWhereInFiles
* @author Faisal Shaikh
* @abstract This project is to find out a part of string from anywhere in any folder
* @version 1.0
* @package anywhereinfiles
*
*
*
*
*************************************************************************/
session_start();
?>
<title>Any where In Files || AnyWhereInFiles.php</title>
<head>
<style>
h1{color: #233E99;}
td{ font-size:11px; font-family:arial;vertical-align:top;border:1px solid #fff;}
a{font-size:11px; font-family:arial;}
.me{font-size:11px; font-family:arial;color:#333;}
</style>
</head>
<h1>AnyWhereInFiles.php</h1>
<form action="<?php
echo $_SERVER['PHP_SELF'];
?>" method="POST">
<table>
<tbody>
<tr>
<td><label for="search">String </label></td>
<td><input id="search" type="text" name="search" placeholder="Enter your string" value="<?php
if (isset($_SESSION['searchString']) && $_SESSION['searchString'] != null)
echo $_SESSION['searchString'];
?>" /></td>
</tr>
<tr>
<td><label for="directory">Folder </label></td>
<td><input id="directory" type="text" name="directory" value="<?php
echo getcwd();
?>" /></td>
</tr>
<tr>
<td><label for="case">Case Sensitive </label></td>
<td><input type="checkbox" name="case" /></td>
</tr>
<tr>
<td><label for="maxtime">Max Execution Time </label></td>
<td><input type="text" name="maxtime" value="30"/></td>
<td>Do not change the value if you do not have an idea about it.</td>
</tr>
<tr>
<td><input type="submit" value="Search the string" /></td>
</tr>
</tbody>
</table>
</form>
<?php
function getDirContents($dir, &$results = array())
{
if ($_POST['search'] == null)
exit;
ini_set('max_execution_time', $_POST['maxtime']);
$_SESSION['searchString'] = $_POST['search'];
echo "<script>var elm = document.getElementById('search');elm.value='$_POST[search]';</script>";
if (!isset($_POST['case']))
$string = strtolower($_POST['search']);
else
$string = $_POST['search'];
$files = scandir($dir);
foreach ($files as $key => $value) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (!is_dir($path)) {
$content = file_get_contents($path);
if (!isset($_POST['case']))
$content = strtolower(file_get_contents($path));
if (strpos($content, $string) !== false) {
echo $path . "<br>";
}
$results[] = $path;
} else if ($value != "." && $value != "..") {
getDirContents($path, $results);
$results[] = $path;
}
}
return $results;
}
if (isset($_POST['directory']))
getDirContents($_POST['directory']);
//---------------------------------------------------------------------------------------------------------------------------------//
//@endof file anywhereinfiles.php
//@note if you have query, need, idea, help; feel free to contact f.shaikh1993@gmail.com
?>
<br/>
<br/>
<span class="me">"AnyWhereInFiles" is a Open Source Project, developed by <a href="mailto:f.shaikh1993@gmail.com">Faisal Shaikh</a> .
<br />
<a href="https://github.com/skfaisal93/AnyWhereInFiles">https://github.com/skfaisal93/AnyWhereInFiles</a>
</span>