Question

Is it possible to exclude hidden files and folders from the readdir() function? I have a directory where there are many folders and some hidden folders. I want to read all folders except the hidden ones.

Thanks for any help.

Kcssm

Was it helpful?

Solution

If you just want to exclude files starting with a dot, ".", you can do something like this:

$files = readdir('/path/to/folder');
$files = array_filter($files, create_function('$a','return ($a[0]!=".");'));

This will only return files that don't start with dot "."

On windows, hidden files work differently, I don't know how to find those out.

OTHER TIPS

Use SPL iterators: DirectoryIterator + FilterIterator.

You can exclude files and folders which starting "." by using following code

$ignoreList = array('cgi-bin', '.', '..', '._');
   if ($directory = opendir(APPPATH . 'controllers/user')) {
  while (false !== ($filename = readdir($directory))) {
    if (!in_array($filename, $ignoreList) and substr($filename, 0, 1) != '.') {
         echo $filename."<br>";
      }
   }
 }

You could also use scandir with preg_grep to hide all files and folders starting with a ".". Please refer below code,

$dir    = '/Users/Umesh/Sites/';
$files = preg_grep('/^([^.])/', scandir($dir));

print_r($files);

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top