Ignore hidden files with php

mattyb picture mattyb · Mar 29, 2011 · Viewed 7.6k times · Source

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.

Answer

Micah Carrick picture Micah Carrick · Mar 29, 2011

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