【问题标题】:Does php scandir() exclude hidden files under Windows?php scandir() 是否排除Windows下的隐藏文件?
【发布时间】:2015-07-29 05:13:09
【问题描述】:

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

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

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

【问题讨论】:

    标签: php windows scandir hidden-files


    【解决方案1】:

    我已经在 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 中工作。

    【讨论】:

      【解决方案2】:

      一个简短的测试表明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 样式并忽略以点开头的文件,如下所示:

      Exclude hidden files from scandir

      【讨论】:

      • 你试过我建议的答案了吗?在 Windows 中,有一种方法可以标记隐藏文件并防止它们被打印到屏幕上,或者根据需要排除它们。
      猜你喜欢
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 1970-01-01
      • 2021-03-08
      • 2022-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多