doudui1850 2013-11-01 10:06
浏览 59

php RecursiveDirectoryIterator:如何用点和双点排除目录路径?

RecursiveDirectoryIterator seems to give me two different results from my localhost and live server,

define ('WEBSITE_DOCROOT', str_replace('\\', '/', dirname(__FILE__)).'/');

print_r(WEBSITE_DOCROOT);

// List all the class directories in the array.
$main_directories = array(
    'core/model/',
    'core/helper/',
    'core/ext/'
);

// Set other vars and arrays.
$sub_directories = array();

// List any sub dirs in the main dirs above and store them in an array.
foreach($main_directories as $path_directory)
{
    $iterator = new RecursiveIteratorIterator
    (
        new RecursiveDirectoryIterator(WEBSITE_DOCROOT.$path_directory), // Must use absolute path to get the files when ajax is used.
        RecursiveIteratorIterator::SELF_FIRST
    );

    foreach ($iterator as $fileObject) 
    {
        if ($fileObject->isDir()) 
        {
            //if($fileObject->isDir() === '.' || $fileObject->isDir() === '..') {continue;} 

            // Must trim off the WEBSITE_DOCROOT. 
            $sub_directories[] = preg_replace('~.*?(?=core|local)~i', '', str_replace('\\', '/', $fileObject->getPathname())) .'/';
        }
    }
}

// Mearge the main dirs with any sub dirs in them.
$merged_directories = array_merge($main_directories,$sub_directories);
print_r($merged_directories);

localhost,

(
    [0] => core/model/
    [1] => core/helper/
    [2] => core/ext/
)

live server,

(
    [0] => core/model/
    [1] => core/helper/
    [2] => core/ext/
    [3] => core/model/./
    [4] => core/model/../
    [5] => core/helper/./
    [6] => core/helper/../
    [7] => core/ext/./
    [8] => core/ext/../
)

So, how can I exclude the directory paths with a dot and double dots?

EDIT:

Live server - PHP version 5.3.27 Localhost - PHP version 5.5

  • 写回答

1条回答 默认 最新

  • douji5523 2013-11-01 12:26
    关注

    Try with :

    new RecursiveDirectoryIterator(WEBSITE_DOCROOT.$path_directory, RecursiveDirectoryIterator::SKIP_DOTS);
    

    See http://us1.php.net/manual/en/class.filesystemiterator.php

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?