PHP:使用 scandir(),文件夹被视为文件
在 Linux CentOS 5.5 上使用 PHP 5.3.3(稳定)。
这是我的文件夹结构:
www/myFolder/
www/myFolder/testFolder/
www/myFolder/testFile.txt
对“myFolder”文件夹使用 scandir() 我得到以下结果:
.
..
testFolder
testFile.txt
我试图从结果中过滤掉文件夹并只返回文件:
$scan = scandir('myFolder');
foreach($scan as $file)
{
if (!is_dir($file))
{
echo $file.'\n';
}
}
预期结果是:
testFile.txt
但是我实际上看到:
testFile.txt
testFolder
谁能告诉我这里出了什么问题吗?
Using PHP 5.3.3 (stable) on Linux CentOS 5.5.
Here's my folder structure:
www/myFolder/
www/myFolder/testFolder/
www/myFolder/testFile.txt
Using scandir() against the "myFolder" folder I get the following results:
.
..
testFolder
testFile.txt
I'm trying to filter out the folders from the results and only return files:
$scan = scandir('myFolder');
foreach($scan as $file)
{
if (!is_dir($file))
{
echo $file.'\n';
}
}
The expected results are:
testFile.txt
However I'm actually seeing:
testFile.txt
testFolder
Can anyone tell me what's going wrong here please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要更改目录或将其附加到您的测试中。当文件不存在时,
is_dir
返回 false。那应该做正确的事
You need to change directory or append it to your test.
is_dir
returns false when the file doesn't exist.That should do the right thing
is_dir() 不是以文件作为参数吗?
Doesn't is_dir() take a file as a parameter?
已经在这里告诉你答案: http://bugs.php.net/bug.php ?id=52471
Already told you the answer here: http://bugs.php.net/bug.php?id=52471
如果您显示错误,您会明白为什么这不起作用:
现在尝试将 $file 传递给 is_dir()
If you were displaying errors, you'd see why this isn't working:
Now try passing $file to is_dir()
如果来到这里的任何人有兴趣将输出保存到数组中,这里有一个快速的方法(修改为更有效):
If anyone who comes here is interested in saving the output to an array, here's a fast way of doing that (modified to be more efficient):