Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore hidden files with php [duplicate]

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.

like image 840
mattyb Avatar asked Dec 01 '25 01:12

mattyb


2 Answers

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);
}
?>
like image 168
Micah Carrick Avatar answered Dec 02 '25 15:12

Micah Carrick


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

if ( !in_array($file, $ignore))
like image 22
Michael Berkowski Avatar answered Dec 02 '25 14:12

Michael Berkowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!