2010-12-18 11 views
1

申し訳ありません。複数のディレクトリに、Filesという1つのメインディレクトリの下に数百種類のファイルがあります。どのように私は、ルートディレクトリ(FIles)andsのすべてのサブディレクトリなどをearchでPHPファイルを作成し、ユーザーに結果を印刷するのだろうか?しかし、私はこの検索エンジンではなく、ファイル名を検索したいだけです。可能であれば、既に作成されたスクリプトやサンプルコードを提供できますか?どうもありがとう! PHPでファイルを検索するPHPの内容ではなく、ファイル名のためにdirectoyをどのように検索しますか?

+0

WHEあなたは「ファイル名を検索する」と言っていますが、そのファイル名を持つファイルをFilesディレクトリとサブディレクトリのどこにでもリストしたいですか?部分的なファイル名の一致が必要ですか(たとえば、 'a_img1.gif'などに一致する' img')か、正確な名前だけでしょうか?あなたは最初の試合で立ち止まる、またはすべての試合をリストしたいですか? – salathe

+1

この質問に追加回答が不要になった場合は、該当する投稿に回答としてマークしてください。 –

答えて

5

、あなたは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

+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

ありがとう!ファイサルの許諾を受けることができますか? –

+0

あなたは無料でそれを使用することができます。 –

関連する問題