2

I am trying to scan a folder of images, however I keep seeing the ._ files the mac created

I am using this code:

   <?php
if ($handle = opendir('assets/automotive')) {
    $ignore = array( 'cgi-bin', '.', '..','._' );
    while (false !== ($file = readdir($handle))) {
        if ( !in_array($file,$ignore)) {
            echo "$file\n";
        }
    }
    closedir($handle);
}
?>

Any ideas as to why? I created a ignore array that covers it.

Update: Still shows both.

mattyb
  • 194
  • 3
  • 12

3 Answers3

7

I think you want to ignore any file that begins with a dot (.) and not just the filename.

<?php
if ($handle = opendir('assets/automotive')) {
    $ignore = array( 'cgi-bin', '.', '..','._' );
    while (false !== ($file = readdir($handle))) {
        if (!in_array($file,$ignore) and substr($file, 0, 1) != '.') {
            echo "$file\n";
        }
    }
    closedir($handle);
}
?>
Micah Carrick
  • 9,967
  • 3
  • 31
  • 43
  • Yup that's the trick, thank you! I will mark it accepted when it lets me – mattyb Mar 29 '11 at 20:17
  • Given that we know `$file` is a non-empty string, `substr($file, 0, 1)` is a needlessly verbose way of writing `$file[0]`. The redundancy of having the `$ignore` array include dotfiles despite them being covered by the other condition is also ugly. – Mark Amery May 30 '15 at 12:51
2

in_array() takes two parameters: the thing you want to find, and the array to search in. You want:

if ( !in_array($file, $ignore))
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
0

You're checking for in_array, but the next questions is: "is what in_array".

in_array needs a second parameter, in this case $file, to look for. You'll need:

in_array($file,$ignore);
Brad Christie
  • 100,477
  • 16
  • 156
  • 200