1

I am using scandir to list all the files in a directory. But there should be an exception for ./, ../ and tmp folder.

I already have this to exclude the dot and double dot:

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

How can i add tmp folder to it? (name of the folder is tmp)

Jack Maessen
  • 1,780
  • 4
  • 19
  • 51
  • As an aside: the code you're using will exclude _any_ file or directory whose name starts with a dot. This may or may not be something you actually want. –  Feb 01 '19 at 21:08
  • i agree.! But a name started with a dot is not allowed. It is filtered in the input – Jack Maessen Feb 01 '19 at 21:10

4 Answers4

1

Try :

   $toRemove = array('.','..','tmp'); 

   $cdir = scandir($dir);
   
   $result = array_diff($cdir, $toRemove);

It's easier than preg_grep

Amine KOUIS
  • 1,686
  • 11
  • 12
Vinz
  • 205
  • 4
  • 10
1

I would choose for this solution, because of already mentioned by @duskwuff, your current code excludes all the files which start with a .

$files = array_diff( scandir($dir), array(".", "..", "tmp") );
mudraya
  • 99
  • 7
0

Since it's a regex you can try to take a look at the negative lookahead:

$files = preg_grep('/^(?!tmp|\.{1,2})$/', scandir($dir));
0

I would have done something like that if you want to stick with regex

$files = preg_grep('/^(?!tmp|(?!([^.]))).*/', scandir($dir));

Dialex
  • 439
  • 2
  • 9