gpt4 book ai didi

php 递归目录迭代器 : how to exclude directory paths with a dot and double dots?

转载 作者:可可西里 更新时间:2023-11-01 13:33:41 26 4
gpt4 key购买 nike

RecursiveDirectoryIterator 似乎从本地主机和实时服务器给我两个不同的结果,

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/
)

实时服务器,

(
[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本地主机 - PHP 5.5 版

最佳答案

尝试:

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

参见 http://us1.php.net/manual/en/class.filesystemiterator.php

关于php 递归目录迭代器 : how to exclude directory paths with a dot and double dots?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19724579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com