【问题标题】:php scandir --> search for files/directoriesphp scandir --> 搜索文件/目录
【发布时间】:2010-05-18 23:17:24
【问题描述】:

在我问之前我搜索过,没有运气..

我正在为自己寻找一个简单的脚本,我可以搜索文件/文件夹。在php手册中找到了这个代码sn-p(我想我需要这个),但它不适合我。

“正在寻找一种使用掩码搜索文件/目录的简单方法。这是这样的功能。

默认情况下,此函数会将scandir()结果保存在内存中,以避免多次扫描同一目录。"

<?php 
function sdir( $path='.', $mask='*', $nocache=0 ){ 
    static $dir = array(); // cache result in memory 
    if ( !isset($dir[$path]) || $nocache) { 
        $dir[$path] = scandir($path); 
    } 
    foreach ($dir[$path] as $i=>$entry) { 
        if ($entry!='.' && $entry!='..' && fnmatch($mask, $entry) ) { 
            $sdir[] = $entry; 
        } 
    } 
    return ($sdir); 
} 
?>

感谢您的帮助,

彼得

【问题讨论】:

    标签: php


    【解决方案1】:
    $a = new RegexIterator(
        new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator('DIRECTORY HERE')
        ),
        '/REGEX HERE/',
        RegexIterator::MATCH
    );
    
    foreach ($a as $v) {
        echo "$v\n"; //$v will be the filename
    }
    

    【讨论】:

    • 感谢您的回答,Artefacto-s 解决方案对我有好处,但是!没有正则表达式是否有类似的解决方案?或者当我只想搜索带/不带掩码的文件名时,什么是正则表达式?例如:stackoverflow.zip,我想搜索“overflow”、“stack”或“stackoverflow”...非常感谢!
    • 为此,您只需要正则表达式overflowstackstackoverflow。这三个将匹配“stackoverflow.zip”。您可以以这种简单的方式使用正则表达式,您只需要小心转义具有特殊含义的字符。
    • 太简单了!!非常感谢!
    • +1 不错且高效的方法。这个问题的主题有点离题,但你能在这里分享你对我的问题的想法吗:stackoverflow.com/questions/3356376/…
    【解决方案2】:

    【讨论】:

      【解决方案3】:

      我只是想搜索一个文件,你可以用这个sn-p:

          <?php   
          $s = $get['s'];
          $e = ".htm";
          $folders = array("data1", "data2", "data3");
          $files = array(); // nothing needed here. anything in this array will be showed as a search result.
          for($i=0;$i<=count($folders)-1;$i++) {
              $glob = glob($folders[$i]);
              $files = array_merge($files, $glob[$i]);
          }
          echo "Search - $s<br><br>";
          if(count($files) == 1) {
              echo "<li><a href='$files[0]'>".heir($files[0])."</a></li>";
          }
          if(count($files) != 1) {
              for($i=0;$i<=count($files)-1;$i++) {
                  echo "<li><a href='$files[$i]'>".heir($files[$i])."</a></li>";  
              }
          }
          if(count($files) == 0) {
              echo "Sorry, no hits.";
          }
      ?>
      

      【讨论】:

        【解决方案4】:

        接受的答案非常好,但它让我想起了 Spl 迭代器。 Fabien Potencier 在这里解释了他是如何在 symfony 中创建 Finder 类的:

        http://fabien.potencier.org/article/43/find-your-files

        我也使用他的查找器类,它们有一个非常好的链式接口。

        示例:

        use Symfony\Component\Finder\Finder;
        
        $finder = new Finder();
        $finder->files()->in(__DIR__);
        
        foreach ($finder as $file) {
           print $file->getRealpath()."\n";
        }
        

        还有..

        $finder->files()->name('*.php');
        // or 
        $finder->files()->size('>= 1K')->size('<= 2K');
        $finder->date('since yesterday');
        

        文档:http://symfony.com/doc/2.0/cookbook/tools/finder.html

        sf1.4框架的PHP5.2+版本: http://svn.symfony-project.com/branches/1.4/lib/util/sfFinder.class.php

        这个版本略有不同,不那么花哨,但也能胜任。不过,您需要创建一个 sfException 类,它是它与 symfony 框架的唯一关联。您可以创建自己的 sfException 类:

        class sfException extends Exception { }
        

        文档可以在这里找到:http://www.symfony-project.org/cookbook/1_2/en/finder

        【讨论】:

          猜你喜欢
          • 2016-05-15
          • 1970-01-01
          • 2010-10-11
          • 2011-04-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多