doulao1934 2012-03-18 02:17
浏览 65
已采纳

php - 用于搜索目录和仅限.jpg的glob

What if I want to search for a only subdirectories AND certain file types. Ex. I have a folder named “stuff” where random stuff is uploaded. And say I want to search for just subfolders AND .jpg files within “stuff” and nothing more. I know to search for only .jpg is…

$array = glob(‘stuff/{*.jpg}’, GLOB_BRACE);

and to search for only subdirectories is…

$array = glob(‘stuff/*’, GLOB_ONLYDIR);

…but how do I combine the two without getting any other unwanted files? Is there a pattern for subirectories for GLOB_BRACE?

  • 写回答

2条回答 默认 最新

  • dongtiao0657 2012-08-29 07:18
    关注

    This recursive function should do the trick:

    function recursiveGlob($dir, $ext) {
        $globFiles = glob("$dir/*.$ext");
        $globDirs  = glob("$dir/*", GLOB_ONLYDIR);
    
        foreach ($globDirs as $dir) {
            recursiveGlob($dir, $ext);
        }
    
        foreach ($globFiles as $file) {
            print "$file
    "; // Replace '
    ' with '<br />' if outputting to browser
        }
    }
    

    Usage: recursiveGlob('C:\Some\Dir', 'jpg');

    If you want it to do other things to the individual file, just replace the print "$file " part.

    The function can be more specific in its glob searches and only match directories as well as files with the specified file extension, but I made it this way for simplicity and transparency.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存