5

Simple question - How to list .htaccess files using glob()?

hakre
  • 178,314
  • 47
  • 389
  • 754
Tony Bogdanov
  • 6,238
  • 10
  • 45
  • 74

2 Answers2

12

glob() does list "hidden" files (files starting with . including the directories . and ..), but only if you explicitly ask it for:

 glob(".*");

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

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

The alternative to glob of course would be just using scandir() and a filter (fnmatch or regex):

 preg_grep('/^\.\w+/', scandir("."))
hakre
  • 178,314
  • 47
  • 389
  • 754
mario
  • 138,064
  • 18
  • 223
  • 277
  • The actual peculiarity is that PHP doesn't just make the `glob.h` system call constant `GLOB_PERIOD` available. So it's more of a circumvention. – mario Dec 22 '11 at 00:08
  • Yeah, added the code, it's quite long to make use of that parameter value format to get those files starting with period. I wonder why the constant has not been integrated. Is that windows loving? – hakre Dec 22 '11 at 00:44
  • I would just call this a bug. Lets take a practical use-case: delete all files, including dot files from a folder: `array_map('unlink', glob(__DIR__."/*/folder/*.*"));`. Expected result: all files gone- but this won't work. A simple wildcard won't work, too: `array_map('unlink', glob(__DIR__."/*/scripts/webinterface/*"));`. One would have to combine the glob patterns to match both: `{.*,*.*}` (dotfiles + all files). And i guess, if i file it as bug i would get expected behavior as response and a direct close... =). – Jens A. Koch May 18 '16 at 16:17
0

in case any body come to here,

since the SPL implemented in PHP, and offers some cool iterators, you may make use of the to list your hidden files such as .htaccess files or it's alternative hidden linux files.

using DirectoryIterator to list all of directory contents and excluding the . 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
    }
}
hassan
  • 7,013
  • 2
  • 20
  • 32
  • I think I am going to go this route for my current project, but ...I have concerns: http://php.net/manual/en/class.directoryiterator.php#122474 Have you experienced any similar obstacles? – mickmackusa Mar 29 '18 at 01:40
  • Just ran some tests on my wamp and `->isDot()` was gobbling up my `.htaccess` files with `.` and `..`. Perhaps if someone wishes to fully investigate and post a Note in the manual it would be helpful to future researchers. http://php.net/manual/en/directoryiterator.isdot.php A literal check for `.` and `..` seems necessary. – mickmackusa Mar 29 '18 at 02:12
  • " Have you experienced any similar obstacles?" Nope, Your code working normally, I had tested it on a Windows server with php5.6 and it works fine, excluding the `.` and `..` and the `.DB_STORE` file in your case, also it does not exclude the .htaccess file – hassan Mar 29 '18 at 11:44
  • Applying your code in a directory with the following contents `.htaccess , .DB_STORE , a.htaccess` gived the following output : https://i.imgur.com/fWWCTa9.png – hassan Mar 29 '18 at 11:46