繁体   English   中英

php scandir() 是否排除 Windows 下的隐藏文件?

[英]Does php scandir() exclude hidden files under Windows?

在 Windows 系统上,备份代理创建了与原始文件名称几乎相同且路径相同的临时隐藏文件。 这可能会干扰使用 PHP scandir()的进程。

现在我想知道 PHP scandir()是否排除了 Windows 上设置了隐藏标志的文件。

有一些关于 Linux 风格的隐藏文件的文章, scandir()应该如何忽略以点开头的文件,但很少有关于 Windows 文件的信息。

我已经在 windows 7 和 windows 8 & 8.1 上尝试过这段代码,它肯定会通过标记出来来排除隐藏的文件。

   <?php

    $location="path/to/a/folder/";

    if( function_exists("scandir") ){

        $file = scandir($location);

        foreach($file as $this_file) {
        /***Putting the condition here restricts hidden files and files starting with '.' and '~'...****/
            if (is_hidden_file($location.$this_file) || $this_file == '.' || $this_file == '..' || $this_file[0]=='.' || $this_file[0]=='~' ) 
                continue;

            else {
            var_dump($this_file);
            }

        }       
    }

    function is_hidden_file($fn) {
        $dir = "\"".$fn."\"";
        $attr = trim(shell_exec("FOR %A IN (".$dir.") DO @ECHO %~aA"));
        if($attr[3] === 'h')
            return true;

        return false;
    }
?>

我看到您在问题中提到有一些方法可以排除以 '.' 开头的文件。 和 linux 中的东西,但关于 windows 的信息很少。 然后检查一下它不仅根除以“。”开头的文件 & '..' 但也会标记出实际隐藏的文件,并且肯定会在 Windows 中工作。

一个简短的测试表明scandir()glob()或其他人都没有处理隐藏标志。

这是实验和结果:

在此处输入图片说明

部分:

  • Windows 7的
  • PHP 5.6.9 (x86)
  • Visual Studio 2012 可再发行 x86

所以scandir()不会隐藏设置了隐藏标志的文件。

下一个问题是,是否可以配置更强大的 PHP 命令,如glob()

首先,没有处理标志的参数:

http://php.net/manual/de/function.glob.php

其次,Gabriel S. Luraschi 有这样一段感人的评论:

http://php.net/manual/de/function.glob.php#110510

他推荐exec('dir ... \\A ...') 但是在商业主机上(如果它们在 Windows 上运行),这是不允许的。

可以肯定的是:使用 Linux 风格并忽略以点开头的文件,如下所示:

从 scandir 中排除隐藏文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM