[英]scandir returns [0] =>. and [1]=>.. in array
I have a simple PHP script with this snippet of code: 我有一个简单的PHP脚本与这段代码:
$files = scandir($dir);
print_r($files);
And noticed in my array i have: 在我的阵列中注意到我:
[0] => .
[1] => ..
[2] => assets.php
[3] => loader.php
Is this a bug on my server or is it expected behavior, what are they? 这是我的服务器上的错误还是预期的行为,它们是什么? And if it's the latter is there any way to exclude them from the scan? 如果是后者有什么方法可以将它们从扫描中排除? As I wish to only get script files that I made from the directory and nothing else. 因为我希望只获取我从目录中创建的脚本文件而没有别的。 Is there any way to exclude them from the scan? 有没有办法将它们从扫描中排除?
No it isn't a bug.... .
不,它是不是一个错误...... .
is the current directory, ..
the parent directory 是当前目录, ..
父目录
The user-contributed notes show one way of excluding them: 用户提供的注释显示了一种排除它们的方法:
$directory = '/path/to/my/directory';
$scanned_directory = array_diff(scandir($directory), array('..', '.'));
If you use other options besides scandir, such as SPL's DirectoryIterator, there's options to exclude them (such as combining it with a RegexIterator), or to test for them as you're iterating: 如果您使用scandir之外的其他选项,例如SPL的DirectoryIterator,可以选择将它们排除(例如将它与RegexIterator结合使用),或者在迭代时测试它们:
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
if (!$fileinfo->isDot()) {
echo $fileinfo->getFilename() . "\n";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.