Home » Php » Exclude hidden files from scandir

Exclude hidden files from scandir

Posted by: admin November 30, 2017 Leave a comment

Questions:

I am using the following code to get a list of images in a directory:

$files = scandir($imagepath);

but $files also includes hidden files. How can I exclude them?

Answers:

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

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

Questions:
Answers:

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

Questions:
Answers:
function nothidden($path) {
    $files = scandir($path);
    foreach($files as $file) {
        if ($file[0] != '.') $nothidden[] = $file;
        return $nothidden;
    }
}

Simply use this function

$files = nothidden($imagepath);

Questions:
Answers:

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 🙂

Questions:
Answers:

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

Questions:
Answers:

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.

Questions:
Answers:

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.

Questions:
Answers:

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>";
}