DirectoryIterator 扫描以排除“.”和'..'目录还包括它们吗?
在下面的脚本中,我尝试将 $base 目录中存在的文件夹复制到 $target 目录。然而,在我最初的回显测试中,它返回 .和 .. 目录,即使我试图在条件中处理该异常。
我缺少什么?
$base = dirname(__FILE__).'/themes/';
$target = dirname( STYLESHEETPATH );
$directory_folders = new DirectoryIterator($base);
foreach ($directory_folders as $folder)
{
if ($folder->getPath() !== '.' && $folder->getPath() !=='..' )
{
echo '<br>getPathname: '. $folder->getPathname();
//copy($folder->getPathname(), $target);
}
}die;
但是,如果我将条件更改为...,这对我来说毫无意义,
if (!is_dir($folder) && $folder->getPath() !== '.' && $folder->getPath() !=='..' )
它会返回 $base 内的正确文件夹。什么?
In the script below, I'm trying to copy the folders that exist in the $base directory over to the $target directory. However, in my initial echo test, its returning the . and .. directories even though I'm trying to handle that exception in the conditional.
What am I missing?
$base = dirname(__FILE__).'/themes/';
$target = dirname( STYLESHEETPATH );
$directory_folders = new DirectoryIterator($base);
foreach ($directory_folders as $folder)
{
if ($folder->getPath() !== '.' && $folder->getPath() !=='..' )
{
echo '<br>getPathname: '. $folder->getPathname();
//copy($folder->getPathname(), $target);
}
}die;
However, and this makes no sense to me, if I change the conditional to...
if (!is_dir($folder) && $folder->getPath() !== '.' && $folder->getPath() !=='..' )
It returns the correct folders inside of $base. What?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DirectoryIterator::getPath()
返回目录的完整路径——而不仅仅是它的最后一部分。如果您只想要该路径的最后一部分,您可能应该使用
SplFileInfo::getBasename()
在您的情况下。或者,对于您的特定测试,您可能需要查看
DirectoryIterator::isDot()
方法(引用):DirectoryIterator::getPath()
returns the full path to the directory -- and not only the last part of it.If you only want the last portion of that path, you should probably use
SplFileInfo::getBasename()
in your condition.Or, for your specific test, you might want to take a look at the
DirectoryIterator::isDot()
method (quoting) :您可以使用 DirectoryIterator::isDot 代替:
You can use
DirectoryIterator::isDot
instead: