开发者

Ignore hidden files with php [duplicate]

开发者 https://www.devze.com 2023-02-20 10:25 出处:网络
This question already has answers here: Exclude hidden files from scandir (11 answers) 开发者_JAVA百科Closed 7 years ago.
This question already has answers here: Exclude hidden files from scandir (11 answers) 开发者_JAVA百科 Closed 7 years ago.

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.


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


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

if ( !in_array($file, $ignore))


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消