2014-02-14 96 views
0

使用此php代码循环访问图像目录时,我需要过滤掉我的MAC dev计算机坚持创建的系统文件。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?"); 
    } 

有没有的,我需要隐藏的系统文件的任何其他名称要过滤掉还是这两个呢?

有没有更好的方式来处理这个问题?

+0

也看到这个问题和答案:http://stackoverflow.com/questions/8532569/exclude-hidden-files-from-scandir-php –

回答

1

您可以替换此:

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

本:

!stristr($entry, '.DS_Store') 

且有.localized,看到了一些地方。正如@deceze所说,unix系统充满了.配置文件,这一切都取决于您正在处理的目录。

甚至更​​好的可以更换所有的if语句是:

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

可以有吨不同的隐藏文件,如.svn.git,以及你的例子上面试试这个...它检查是否第一个字符是一个.

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

Dot files”,以点开头的文件,是UNIX系统中的“隐藏”文件长期约定。他们有很多是由各种程序创建的。去终端,并与ls -a玩。除非你知道你在做什么,否则公约就是要意识到它们并忽略它们。因此,忽略全部文件以.开头。