php scandir产生额外的元素(2个点) [英] php scandir produces extra elements (2 dots)
问题描述
我在名为content的目录中有以下文件:仅index.php,page1.php,page2.php和page3.php.
I have the following files in a directory called content: index.php, page1.php, page2.php and page3.php ONLY.
然后我有以下代码:
$column = scandir("content");
foreach ($column as $value) {
$stvalue = str_replace(".php", "", $value);
echo "<div>$stvalue</div>";
}
尽管如此,这是我得到的:
Nevertheless, this is what I get:
<div>.</div>
<div>..</div>
<div>index</div>
<div>page1</div>
<div>page2</div>
<div>page3</div>
前两个元素是什么?我没有这样命名的文件,所以我不知道.
Whats with the first 2 elements? I dont have files named like that so I dont get it.
谢谢.
推荐答案
. -是引用当前目录的特殊目录.
. - is a special directory referencing the current directory.
..-也是一个特殊目录,它引用父目录.
.. - is also a special directory and its referencing the parent directory.
要删除特殊目录,我可以想到一些选项:
To remove the special directories I can think of some options:
1.
foreach(glob("*.php") as $filename) {
echo "<div>$filename</div>";
}
2.
$files = array_diff(scandir("content"), array('..', '.'));
foreach($files as $file) { ... }
3.
foreach ($files as $file) {
if($file != '.' and $file != '..') { ... }
}
以上所有都是替代方案.如果使用glob()
,则无需使用scandir()
,反之亦然. glob()
-需要一个模式.也可以为它提供这样的路径:
glob("[path]/*.php")
-这将列出路径中的所有php文件. glob()
文档可在此处找到 PHP-glob()
All of the above are alternatives. You don't need to use scandir()
if you use glob()
and vice versa. glob()
- expects a pattern. It is possible to also provide it with the path like this:
glob("[path]/*.php")
- this will list any php file located in path. glob()
documentation can be found here PHP - glob()
这篇关于php scandir产生额外的元素(2个点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!