dpepbjp126917 2014-06-25 17:11
浏览 62
已采纳

如何返回特定文件夹名称及其路径的实例?

I took a look at recursive searching in PHP (see here), and it's really helped my understanding of what I want to do. However, I'm hitting a minor roadblock.

What I want to do is use this recursive search to return the path of a specific folder name that is in multiple locations. Once that folder name is found, it would execute another function. This would continue until all folders with that name are found.

I have some code from a similar question posted here as a base, but I'm a little confused on how to phrase it as such to "look" for a certain folder name. What is the best way to do that?

  • 写回答

1条回答 默认 最新

  • dongpi3237 2014-06-25 18:02
    关注

    An exemple that works (in other words, there is probably a more elegant way to do it):

    function myfunction($param) {
        echo $param . PHP_EOL;
    }
    
    $path = realpath('/myrootpath');
    $target = 'myfolder';
    
    $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path),
                                             RecursiveIteratorIterator::SELF_FIRST,
                                             RecursiveIteratorIterator::CATCH_GET_CHILD);
    foreach($objects as $name => $object){
        if (preg_match('~^.+/' . $target .'(?=/\.$)~', $object, $match))    
            myfunction($match[0]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统