Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php RecursiveDirectoryIterator:如何排除带点和双点的目录路径?_Php_Iterator_Spl - Fatal编程技术网

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

php RecursiveDirectoryIterator:如何排除带点和双点的目录路径?,php,iterator,spl,Php,Iterator,Spl,RecursiveDirectoryIterator似乎从我的本地主机和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/mod

RecursiveDirectoryIterator
似乎从我的本地主机和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);
(
    [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/../
)
本地主机

(
    [0] => core/model/
    [1] => core/helper/
    [2] => core/ext/
)
实时服务器

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);
(
    [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/../
)
那么,如何排除带有一个点和双点的目录路径呢

编辑:

实时服务器-PHP版本5.3.27 Localhost-PHP 5.5版

尝试:

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

太好了!我在寻找同样的东西,我手动排除它们(如果dot/dotdot继续),那么我想我们需要更多关于您的服务器、您正在运行的PHP版本等的信息(因此更新您的问题)PHP.net声明PHP5>=5.3.0。我试过使用PHP5.3.26,效果很好。你可以再试一次吗/它可以工作-避免点,问题可能在代码的其他地方(我尝试递归压缩目录-但这是另一个主题)