好吧我在一個名爲Files的主目錄下的多個目錄中有幾百個不同的文件。我將如何在根目錄(FIles)內創建一個PHP文件並查找所有子目錄等,然後將結果輸出給用戶?但是,我只希望這個搜索引擎搜索文件名,而不是它的內容。如果可能的話,你可以提供已經制作的腳本或代碼示例嗎?非常感謝!我如何搜索directoy的文件名,而不是PHP中的內容?
1
A
回答
5
要搜索的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 />";
}
自定義遞歸水珠
來源: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;
}
2
PHP5:
$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";
}
1
<?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 [email protected]
?>
<br/>
<br/>
<span class="me">"AnyWhereInFiles" is a Open Source Project, developed by <a href="mailto:[email protected]">Faisal Shaikh</a> .
<br />
<a href="https://github.com/skfaisal93/AnyWhereInFiles">https://github.com/skfaisal93/AnyWhereInFiles</a>
</span>
+0
謝謝!我可以詢問這是什麼執照,費薩爾? – 2016-01-05 08:55:20
+0
你可以免費使用它。 – 2016-04-27 10:47:02
相關問題
- 1. 如何在文件搜索時顯示文件內容而不是文件名
- 2. 我如何搜索HTML中的內容而不是標籤
- 3. PHP - 搜索文件的內容
- 4. 搜索文件的內容
- 5. FileReader/Scanner讀取文件名,而不是.txt文件的內容
- 6. 在內容的任何位置搜索而不是啓動
- 7. 如何在Ubuntu Linux中搜索文件內的內容?
- 8. 如何搜索多個pdf文件的內容並返回pdf的文件名?
- 9. 搜索按文件的名稱和打印內容文件夾
- 10. 搜索文件內容
- 11. 如何搜索Perforce軟件倉庫(P4V)中的文件內容?
- 12. 我如何使在Linux中的文件內搜索文本的別名
- 13. 重命名PHP文件夾中的文件,而不是php root
- 14. 如何搜索文件內容中的單詞?
- 15. 如何搜索和匹配文件中的內容?
- 16. 如何允許用戶搜索C文件中的內容?
- 17. 如何在lua中搜索文件的內容
- 18. 如何(遞歸)搜索Windows 7中的所有文件內容?
- 19. MySQL:搜索文件內容的最佳方式(全文搜索)
- 20. 可搜索的文件內容wordpress
- 21. XML文件內容的彈性搜索
- 22. 如何在文件中搜索txt而不是字符
- 23. 我如何使用Zend工作室搜索文件內容RSE
- 24. PHP還是Apache? zip文件的內容是diplayed而不是下載zip文件
- 25. 如何僅使用標籤搜索liferay中的內容(而不是當關鍵字在內容中可用時)?
- 26. 獲取文件名,而不是內容與的FtpWebRequest
- 27. 在c#中搜索文件內容#
- 28. 如何在我的搜索文本框中搜索我的ID,中間名,姓?
- 29. 搜索文件中的某些字符串,而不循環遍歷內容
- 30. 如何使搜索引擎更喜歡更新的內容,而不是檔案?
WHE ñ你說「搜索文件名」是否要列出在Files目錄和子目錄中的任何位置具有該文件名的文件?是否需要部分文件名匹配(例如'img'匹配'a_img1.gif'等)或只是確切的名稱?你想在第一場比賽中停止,還是列出所有比賽? – salathe 2010-12-20 15:33:36
如果此問題不再需要其他答案,請將相應的帖子標記爲答案。 – 2011-04-20 18:29:08