Exclude hidden files from scandir

不打扰是莪最后的温柔 提交于 2019-11-27 14:56:34
mario

On Unix, you can use preg_grep to filter out filenames that start with a dot:

$files = preg_grep('/^([^.])/', scandir($imagepath));

I tend to use DirectoryIterator for things like this which provides a simple method for ignoring dot files:

$path = '/your/path';
foreach (new DirectoryIterator($path) as $fileInfo) {
    if($fileInfo->isDot()) continue;
    $file =  $path.$fileInfo->getFilename();
}
function nothidden($path) {
    $files = scandir($path);
    foreach($files as $file) {
        if ($file[0] != '.') $nothidden[] = $file;
        return $nothidden;
    }
}

Simply use this function

$files = nothidden($imagepath);

I encountered a comment from php.net, specifically for Windows systems: http://php.net/manual/en/function.filetype.php#87161

Quoting here for archive purposes:

I use the CLI version of PHP on Windows Vista. Here's how to determine if a file is marked "hidden" by NTFS:

function is_hidden_file($fn) {

    $attr = trim(exec('FOR %A IN ("'.$fn.'") DO @ECHO %~aA'));

    if($attr[3] === 'h')
        return true;

    return false;
}

Changing if($attr[3] === 'h') to if($attr[4] === 's') will check for system files.

This should work on any Windows OS that provides DOS shell commands.

I reckon because you are trying to 'filter' out the hidden files, it makes more sense and looks best to do this...

$items = array_filter(scandir($directory), function ($item) {
    return 0 !== strpos($item, '.');
});

I'd also not call the variable $files as it implies that it only contains files, but you could in fact get directories as well...in some instances :)

use preg_grep to exclude files name with special characters for e.g.

$dir = "images/";
$files = preg_grep('/^([^.])/', scandir($dir));

http://php.net/manual/en/function.preg-grep.php

Agnel Vishal
$files = array_diff(scandir($imagepath), array('..', '.'));

or

$files = array_slice(scandir($imagepath), 2);

might be faster than

$files = preg_grep('/^([^.])/', scandir($imagepath));

Assuming the hidden files start with a . you can do something like this when outputting:

foreach($files as $file) {
    if(strpos($file, '.') !== (int) 0) {
        echo $file;
    }
}

Now you check for every item if there is no . as the first character, and if not it echos you like you would do.

Use the following code if you like to reset the array index too and set the order:

$path = "the/path";
$files = array_values(
    preg_grep(
        '/^([^.])/', 
        scandir($path, SCANDIR_SORT_ASCENDING)
));

One line:

$path = "daten/kundenimporte/";
$files = array_values(preg_grep('/^([^.])/', scandir($path, SCANDIR_SORT_ASCENDING)));

I am still leaving the checkmark for seengee's solution and I would have posted a comment below for a slight correction to his solution.

His solution masks the directories(. and ..) but does not mask hidden files like .htaccess

A minor tweak solves the problem:

foreach(new DirectoryIterator($curDir) as $fileInfo) {
    //Check for something like .htaccess in addition to . and ..
    $fileName = $fileInfo->getFileName();
    if(strlen(strstr($fileName, '.', true)) < 1) continue;

     echo "<h3>" . $fileName . "</h3>";
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!