简体   繁体   English

递归目录searcer PHP

[英]Recursive directory searcer PHP

我如何编写一个带有geven字符串并返回整个路径以及php中文件名的递归目录搜索器?

The php function dir will help you. php函数dir将为您提供帮助。

http://php.net/manual/en/class.dir.php http://php.net/manual/en/class.dir.php

There is an example in the notes of the docs (by sojka at online-forum dot net) that shows doing this, which I have included below... 在文档的注释中有一个示例(由在线论坛点网的sojka撰写)显示了执行此操作的过程,我在下面包括了...

<?php
public static function getTreeFolders($sRootPath = UPLOAD_PATH_PROJECT, $iDepth = 0) {
      $iDepth++;
      $aDirs = array();
      $oDir = dir($sRootPath);
      while(($sDir = $oDir->read()) !== false) {
        if($sDir != '.' && $sDir != '..' && is_dir($sRootPath.$sDir)) {
          $aDirs[$iDepth]['sName'][] = $sDir;
          $aDirs[$iDepth]['aSub'][]  = self::getTreeFolders($sRootPath.$sDir.'/',$iDepth);
        }
      }
      $oDir->close();
      return empty($aDirs) ? false : $aDirs;
}
?>

There are lots of other similar examples from other people on the same page, so find one that you like and go from there... 同一页面上还有许多其他人的其他类似示例,因此请找到您喜欢的示例并从那里开始...

You could iterate trough the dirctory using a RecursiveDirectoryIterator. 您可以使用RecursiveDirectoryIterator遍历目录。 Look at this: 看这个:

$search_query = "test";
$directory = new RecursiveDirectoryIterator('/path/');
$iterator = new RecursiveIteratorIterator($directory);
$result = new RegexIterator($iterator, '/^.+\'.$search_query.'$/i', RecursiveRegexIterator::GET_MATCH);

Then (array) $result should usually contain all filenames in which 'test' was found under '/path/'. 然后, (array) $result通常应包含在“ / path /”下找到“ test”的所有文件名。

Hope it helps ;) 希望能帮助到你 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM