PHP glob () does not find .htaccess

A simple question: how to list .htaccess files using glob() ?

+4
source share
2 answers

glob() contains a list of hidden files (files starting with . , including directories . and .. ), but only if you explicitly ask it:

  glob(".*"); 

Filtering the returned glob() array for .htaccess entries with preg_grep :

  $files = glob(".*") AND $files = preg_grep('/\.htaccess$/', $files); 

An alternative to glob, of course, would be to simply use scandir() and a filter ( fnmatch or regular expression):

  preg_grep('/^\.\w+/', scandir(".")) 
+11
source

if any body comes here,

since SPL is implemented in PHP and offers some cool iterators, you can use the list to display your hidden files, such as .htaccess or alternative hidden linux files.

using DirectoryIterator to display all directory contents and exclude . and .. as follows:

 $path = 'path/to/dir'; $files = new DirectoryIterator($path); foreach ($files as $file) { // excluding the . and .. if ($file->isDot() === false) { // make some stuff } } 
0
source

Source: https://habr.com/ru/post/1387436/


All Articles