Exclude hidden files from scandir

Solution 1:

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

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

Solution 2:

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

Solution 3:

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

or

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

might be faster than

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

Solution 4:

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

Simply use this function

$files = nothidden($imagepath);