0

While looping through a directory for images using this php code, I need to filter out the system files that my MAC dev computer insists on creating.

First I only had to filter out

.DS_Store 

files, lately there has appeared files looking like this as well:

._.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?");
    }  

Is there any other name of hidden system files that I need to filter out or are these two it?

Is there any better way of handling this than the way I do it?

Matt Welander
  • 8,234
  • 24
  • 88
  • 138
  • Also see this question and answers: http://stackoverflow.com/questions/8532569/exclude-hidden-files-from-scandir-php – Paul Bissex Feb 14 '14 at 19:59

3 Answers3

1

You can replace this:

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

by this:

 !stristr($entry, '.DS_Store')

and there is .localized, seen it some places. As @deceze said, unix systems are full of . configuration files, it all depends on the directory you're working on.

Or even better you can replace all your if statement by:

if(substr($entry, 0, 1)!='.')
CodeBird
  • 3,883
  • 2
  • 20
  • 35
1

"Dot files", files starting with a dot, are a long standing convention of UNIX systems for "hidden" files. There are tons of them created by all sorts of programs. Go to the Terminal and play around with ls -a. The convention is to be aware of them and ignore them unless you know what you're doing. So, ignore all files starting with a ..

deceze
  • 510,633
  • 85
  • 743
  • 889
0

There can be tons of different hidden files, such as .svn, .git, as well as your examples above try this ... it checks to see if the first character is a .

if($entry[0] != '.'){
   // do something as its not a hidden file
}
cmorrissey
  • 8,493
  • 2
  • 23
  • 27