2016-10-31 12 views
0

AJAXとテキスト入力を使用して現在のPHPファイルに検索文字列を送信していますが、このスクリプトはディレクトリ内でファイルとフォルダを検索します。私が望むのは、ディレクトリとサブディレクトリ内のファイルのみを検索してリストするものです。このような構造:サブディレクトリを再帰的に検索し、ディレクトリを無視するPHP

Corecube 
-Categories 
--Movies 
---File 1 
---File 2 
---File 3 
--Music 
---File 1 
---File 2 
---File 3 
--Pictures 
---File 1 
---File 2 
---File 3 
-MainCategories 
--Code 
---File 1 
---File 2 
---File 3 
--Gifs 
---File 1 
---File 2 
---File 3 

とは、検索は「Corecube」の親で行われますが、実際のディレクトリなく、各サブディレクトリからファイル名だけを出してくれる。私は現在のスクリプトのいくつかの反復を試みましたが、毎回それを壊してしまいます。

<?php 
    $dir = "../corecube"; 
    $key = $_GET['key']; 

    // Open a known directory, and proceed to read its contents 
    if (is_dir($dir)) { 
     if ($dh = opendir($dir)) { 
     while (($file = readdir($dh)) !== false) { 
      if($file == $key){ 
      echo('<a href="'.$dir . $file.'">'. $file .'</a>'."\n"); 
      } 
     } 
     closedir($dh); 
     } 
    } 
?> 

答えて

-1

更新されたコードFORMAT:

あなたは、このスクリプトはdownvotedた働いた理由

この

<?php 
function matchFile($filename, $path, $key) 
{ 
    if(preg_match("/$key/i",$filename)) 
    {  
     echo('<a href="'.$path.'">'. $filename .'</a>'."<br>"); //use regex than casual '==' for flexible search  
    } 
} 

function getListDir($dir, $key) 
{ 
    if(strlen($key)<2) die("insert min 2 length string"); //for avoid return/display bunch of list file  
    if(is_dir($dir))  
    {  
     if ($dh = opendir($dir)) 
     { 
      while (($file = readdir($dh)) !== false) 
      { 
       $newItem="$dir/$file"; 
       if($file=='.' || $file=="..") 
       { 
        continue; 
       } 
       elseif(is_dir("$newItem")) 
       { 
        getListDir("$newItem", $key); 
       } 
       elseif(is_file("$newItem")) 
       { 
        matchFile($file, $newItem, $key); 
       } 
       else 
       { 
        //do nothing 
       } 
      }   
     }   
     else    
     { 
      die ("cant open folder: $folder");   
     } 
    } 
} 

//run script 
$initialFolder="initialfolder"; // change this into "../corecube" 
$key = (isset($_GET["key"]))?$_GET['key']:NULL; 
getListDir($initialFolder, $key); 
?> 
+0

のthatsを試してみてください、ネストされたフォルダ内のファイルを検索する必要があります。 Androidアプリから投稿するとコードフォーマットが変更される可能性があります。今私はすでにそれを修正 – plonknimbuzz

関連する問題