2010-12-18 4 views
1

좋아요. 하나의 주 디렉토리 아래에 여러 개의 디렉토리에 수백 개의 다른 파일이 있습니다. 어떻게 루트 디렉토리 (FIles) 안에 PHP 파일을 만들고 모든 하위 디렉토리를 조사한 다음 그 결과를 사용자에게 출력합니까? 그러나, 나는이 검색 엔진이 내용이 아닌 파일 이름만을 검색하기를 원합니다. 가능한 경우 이미 작성된 스크립트 또는 샘플 코드를 제공 할 수 있습니까? 고마워요! PHP에서 파일을 검색하려면directoy에서 PHP의 내용이 아닌 파일 이름을 검색하려면 어떻게해야합니까?

+0

WHE n "파일 이름 검색"이라고 말하면 파일 디렉토리와 하위 디렉토리 어디에서나 해당 파일 이름을 가진 파일을 나열 하시겠습니까? 부분적인 파일명 일치가 필요합니까 (예 :'a_img1.gif'와 일치시키기위한'img' 등) 또는 정확한 이름일까요? 첫 경기에서 멈추거나 모든 경기를 나열 하시겠습니까? – salathe

+1

이 질문에 더 이상 답변이 필요하지 않은 경우 해당 게시물을 답변으로 표시하십시오. –

답변

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; 
} 
+0

이 함수를 사용하는 몇 가지 예제에 대한 설명 섹션을 살펴보십시오 – ifaour

+0

저에게 가장 잘 맞는 예제 코드를 제공해 주시겠습니까? –

+0

질문에서와 같이 하위 디렉토리를 검색하지 않습니다. – salathe

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> 

원래 프로젝트 : https://github.com/skfaisal93/AnyWhereInFiles

+0

감사! 파이살 (Faisal)이 현재 어떤 라이센스를 갖고 있는지 문의 할 수 있습니까? –

+0

무료로 사용할 수 있습니다. –

관련 문제