【问题标题】:MAC hidden system filesMAC隐藏系统文件
【发布时间】:2014-02-14 19:10:52
【问题描述】:

在使用此 php 代码遍历图像目录时,我需要过滤掉我的 MAC 开发计算机坚持创建的系统文件。

首先我只需要过滤掉

.DS_Store 

文件,最近也出现了类似这样的文件:

._.DS_Store


    if ($handle = opendir($server_dir)) { //if we could open the directory...
        while (false !== ($entry = readdir($handle))) { //then loop through it...
            if ( ($entry != ".") && ($entry != "..") && (strpos($entry, ".DS_Store") !== false) && (strpos($entry, "._.DS_Store") !== false) ) { //and skip the . and .. entities
                $m .= '
                '.$client_dir.$entry.'
                ';
            }            
        }
        closedir($handle);      
    }
    else {
        echo("Can't open $dir, does it exist? Does www-user have permission to read it?");
    }  

我需要过滤掉其他名称的隐藏系统文件还是这两个?

有没有比我这样做更好的处理方式?

【问题讨论】:

标签: php macos file loops


【解决方案1】:

你可以替换这个:

 (strpos($entry, ".DS_Store") !== false) && (strpos($entry, "._.DS_Store") !== false)

通过这个:

 !stristr($entry, '.DS_Store')

还有.localized,在一些地方见过。正如@deceze 所说,unix 系统充满了. 配置文件,这完全取决于您正在处理的目录。

或者更好的是,您可以将所有 if 语句替换为:

if(substr($entry, 0, 1)!='.')

【讨论】:

    【解决方案2】:

    Dot files”,以点开头的文件,是 UNIX 系统“隐藏”文件的长期约定。各种程序创建了大量的它们。转到终端并使用ls -a。惯例是要注意它们并忽略它们,除非您知道自己在做什么。因此,忽略以. 开头的所有 文件。

    【讨论】:

      【解决方案3】:

      可能有大量不同的隐藏文件,例如.svn.git,以及上面的示例尝试这个...它检查第一个字符是否是.

      if($entry[0] != '.'){
         // do something as its not a hidden file
      }
      

      【讨论】:

        猜你喜欢
        • 2011-12-11
        • 1970-01-01
        • 2013-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多