PHP:使用scandir(),文件夹被视为文件 [英] PHP: Using scandir(), folders are treated as files

查看:66
本文介绍了PHP:使用scandir(),文件夹被视为文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux CentOS 5.5上使用PHP 5.3.3(稳定).

Using PHP 5.3.3 (stable) on Linux CentOS 5.5.

这是我的文件夹结构:

www/myFolder/
www/myFolder/testFolder/
www/myFolder/testFile.txt

对"myFolder"文件夹使用scandir()会得到以下结果:

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';
    }
}

预期结果是:

testFile.txt

但是我实际上看到的是

testFile.txt
testFolder

有人可以告诉我这是怎么回事吗?

Can anyone tell me what's going wrong here please?

推荐答案

您需要更改目录或将其附加到测试中.当文件不存在时,is_dir返回false.

You need to change directory or append it to your test. is_dir returns false when the file doesn't exist.

$scan = scandir('myFolder');

foreach($scan as $file)
{
    if (!is_dir("myFolder/$file"))
    {
        echo $file.'\n';
    }
}

那应该做正确的事

这篇关于PHP:使用scandir(),文件夹被视为文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆