简体   繁体   English

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

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

On a Windows system, a backup agent created temporary hidden files of nearly the same name as the original files, and in the same path.在 Windows 系统上,备份代理创建了与原始文件名称几乎相同且路径相同的临时隐藏文件。 This was probably disturbing a process that used PHP scandir() .这可能会干扰使用 PHP scandir()的进程。

Now I was wondering if files on Windows having the hidden flag set are excluded by PHP scandir() .现在我想知道 PHP scandir()是否排除了 Windows 上设置了隐藏标志的文件。

There are some articles about the hidden files in Linux style, how scandir() should ignore files that start with a dot, but there is rarely no info about Windows files.有一些关于 Linux 风格的隐藏文件的文章, scandir()应该如何忽略以点开头的文件,但很少有关于 Windows 文件的信息。

I have tried this code on windows 7 and windows 8 & 8.1 and it surely excludes hidden files by flagging them out.我已经在 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;
    }
?>

I see that you have mentioned in the question that there are ways to exclude files starting with '.'我看到您在问题中提到有一些方法可以排除以 '.' 开头的文件。 and stuff in linux but very less information about windows.和 linux 中的东西,但关于 windows 的信息很少。 Then check this out it not only eradicates files starting with '.'然后检查一下它不仅根除以“。”开头的文件& '..' but also flags out the actually hidden files and will work for sure in windows. & '..' 但也会标记出实际隐藏的文件,并且肯定会在 Windows 中工作。

A short test shows that neither scandir() nor glob() or others take care of the hidden flag.一个简短的测试表明scandir()glob()或其他人都没有处理隐藏标志。

Here is the experiment and the result:这是实验和结果:

在此处输入图片说明

Parts:部分:

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

So scandir() does not hide files having the hidden flag set.所以scandir()不会隐藏设置了隐藏标志的文件。

Next question is, can more powerful PHP commands like glob() be configured.下一个问题是,是否可以配置更强大的 PHP 命令,如glob()

Firstly, there is no parameter to deal with flags:首先,没有处理标志的参数:

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

Secondly, there is this telling comment of Gabriel S. Luraschi:其次,Gabriel S. Luraschi 有这样一段感人的评论:

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

He recommends exec('dir ... \\A ...') .他推荐exec('dir ... \\A ...') But on commercial hostings (if they run on Windows), this will not be allowed.但是在商业主机上(如果它们在 Windows 上运行),这是不允许的。

To be sure: use the Linux style and ignore files that start with a dot, like here:可以肯定的是:使用 Linux 风格并忽略以点开头的文件,如下所示:

Exclude hidden files from scandir 从 scandir 中排除隐藏文件

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

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