2010-12-18 24 views
1

好吧我在一個名爲Files的主目錄下的多個目錄中有幾百個不同的文件。我將如何在根目錄(FIles)內創建一個PHP文件並查找所有子目錄等,然後將結果輸出給用戶?但是,我只希望這個搜索引擎搜索文件名,而不是它的內容。如果可能的話,你可以提供已經制作的腳本或代碼示例嗎?非常感謝!我如何搜索directoy的文件名,而不是PHP中的內容?

+0

WHE ñ你說「搜索文件名」是否要列出在Files目錄和子目錄中的任何位置具有該文件名的文件?是否需要部分文件名匹配(例如'img'匹配'a_img1.gif'等)或只是確切的名稱?你想在第一場比賽中停止,還是列出所有比賽? – salathe 2010-12-20 15:33:36

+1

如果此問題不再需要其他答案,請將相應的帖子標記爲答案。 – 2011-04-20 18:29:08

回答

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

查看使用此功能的一些示例的註釋部分 – ifaour 2010-12-18 22:09:57

+0

您能否提供一些最適合我的示例代碼? – 2010-12-18 22:15:06

+0

正如問題中所述,這不會搜索子目錄。 – salathe 2010-12-20 10:49:32

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

謝謝!我可以詢問這是什麼執照,費薩爾? – 2016-01-05 08:55:20

+0

你可以免費使用它。 – 2016-04-27 10:47:02

相關問題