Question

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?

Was it helpful?

Solution

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

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

OTHER TIPS

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);
$files = array_diff(scandir($imagepath), array('..', '.'));

or

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

might be faster than

$files = preg_grep('/^([^.])/', scandir($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

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

scandir() is a built-in function, which by default select hidden file as well, if your directory has only . & .. hidden files then try selecting files

$files = array_diff(scandir("path/of/dir"),array(".","..")) //can add other hidden file if don't want to consider

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>";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top