좋아요. 하나의 주 디렉토리 아래에 여러 개의 디렉토리에 수백 개의 다른 파일이 있습니다. 어떻게 루트 디렉토리 (FIles) 안에 PHP 파일을 만들고 모든 하위 디렉토리를 조사한 다음 그 결과를 사용자에게 출력합니까? 그러나, 나는이 검색 엔진이 내용이 아닌 파일 이름만을 검색하기를 원합니다. 가능한 경우 이미 작성된 스크립트 또는 샘플 코드를 제공 할 수 있습니까? 고마워요! PHP에서 파일을 검색하려면directoy에서 PHP의 내용이 아닌 파일 이름을 검색하려면 어떻게해야합니까?
1
A
답변
5
, 당신은 glob
를 사용할 수 있습니다
http://php.net/manual/en/function.scandir.php
예 :
http://php.net/manual/en/function.glob.php
이 경로를 나열하는 파일을 얻으려면, 당신은 scandir
을 사용할 수 있습니다
$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
감사! 파이살 (Faisal)이 현재 어떤 라이센스를 갖고 있는지 문의 할 수 있습니까? –
+0
무료로 사용할 수 있습니다. –
관련 문제
- 1. Powershell을 사용하여 재귀 디렉터리 이름을 검색하려면 어떻게해야합니까?
- 2. Postgres/SQL에서 글자로 시작하지 않는 이름을 검색하려면 어떻게해야합니까?
- 3. 디렉토리의 파일 수를 효율적으로 검색하려면 어떻게해야합니까?
- 4. HttpModule에서 응답 html을 검색하려면 어떻게해야합니까?
- 5. Rails에서 날짜 범위별로 검색하려면 어떻게해야합니까?
- 6. resx 파일에서 이미지를 검색하려면 어떻게해야합니까?
- 7. mysql에서 제목을 검색하려면 어떻게해야합니까?
- 8. AppleScript로 iTunes를 검색하려면 어떻게해야합니까?
- 9. 개체의 값을 검색하려면 어떻게해야합니까?
- 10. 제스처를 저장하고 검색하려면 어떻게해야합니까?
- 11. JavaScript로 브라우저를 검색하려면 어떻게해야합니까?
- 12. _id로 문서를 검색하려면 어떻게해야합니까?
- 13. 2 단어 개발자 이름을 기반으로 Android 마켓을 검색하려면 어떻게해야합니까?
- 14. 문자열에서 다중 언어를 검색하려면 어떻게해야합니까?
- 15. 젠드가 아닌 PHP의 Zend_Session_Namespace
- 16. ASP.NET 스트림 내용이 파일이 아닌 메모리의
- 17. 컴파일 된 메소드의 소스 파일을 검색하려면 어떻게해야합니까?
- 18. Mercurial Repository에서 서브 디렉토리 만 검색하려면 어떻게해야합니까?
- 19. 캐슬 NVelocity 변수 내용이 아닌 변수 포함
- 20. .jar 라이브러리를 내용이 아닌 "참조"로 테스트합니다.
- 21. Localization.string 파일 내용이
- 22. XML 파일에서 데이터를 검색하려면 어떻게해야합니까?
- 23. .module 파일의 내용을 검색하려면 어떻게해야합니까?
- 24. 단일 날짜로 mysql을 검색하려면 어떻게해야합니까?
- 25. NSDictionary의 모든 내용을 검색하려면 어떻게해야합니까?
- 26. MongoDB에서 문자열의 일부를 검색하려면 어떻게해야합니까?
- 27. Perl에서 터미널 폭을 검색하려면 어떻게해야합니까?
- 28. JPA로 여러 객체를 검색하려면 어떻게해야합니까?
- 29. Google 검색 기록을 검색하려면 어떻게해야합니까?
- 30. Cabal에서 외부 프로그램을 검색하려면 어떻게해야합니까?
WHE n "파일 이름 검색"이라고 말하면 파일 디렉토리와 하위 디렉토리 어디에서나 해당 파일 이름을 가진 파일을 나열 하시겠습니까? 부분적인 파일명 일치가 필요합니까 (예 :'a_img1.gif'와 일치시키기위한'img' 등) 또는 정확한 이름일까요? 첫 경기에서 멈추거나 모든 경기를 나열 하시겠습니까? – salathe
이 질문에 더 이상 답변이 필요하지 않은 경우 해당 게시물을 답변으로 표시하십시오. –