44

I know that glob can look for all files or only all directories inside a folder :

echo "All files:\n";
$all = glob("/*");
var_dump($all);

echo "Only directories\n";
$dirs = glob("/*", GLOB_ONLYDIR);
var_dump($dirs);

But I didn't found something to find only files in a single line efficiently.

$files = array_diff(glob("/*"), glob("/*", GLOB_ONLYDIR));

Works well but reads directory twice (even if there are some optimizations that make the second browsing quicker).

1
  • 1
    you can glob("*.*") this doesn't work though if a folder contains a . in it, or a file doesn't have an extension. Dec 29, 2012 at 18:17

8 Answers 8

63

I finally found a solution :

echo "Only files\n";
$files = array_filter(glob("/*"), 'is_file');
var_dump($files);

But take care, array_filter will preserve numeric keys : use array_values if you need to reindex the array.

1
  • 1
    Nice! Using foreach loop will resolve the issue of preserved numeric keys from array_filter.
    – Imdad
    Nov 27, 2015 at 11:30
7

You can use GLOB_BRACE to match documents against a list of known file extensions:

$files = glob("/path/to/directory/*.{jpg,gif,png,html,htm,php,ini}", GLOB_BRACE);

see: http://www.electrictoolbox.com/php-glob-find-files/

2
  • 10
    Will work in most cases, but take care as directories can also be named toto.jpg if you wish.
    – Alain
    Jun 10, 2014 at 20:06
  • I learned that having a space between the file types in the brace does in fact matter. Don't do it like I did and save yourself some time!
    – Joe McLean
    Mar 17 at 1:59
7

There is an easier way, just one line:

$files = glob("/path/to/directory/*.{*}", GLOB_BRACE);

the {*} means all file endings, so every file, but no folder!

2
  • 2
    Nope, {} replaces any character on files or folders without distinction.
    – Alain
    Jun 19, 2015 at 14:19
  • 9
    Also note that folders can have dot in their name
    – Nux
    Apr 16, 2019 at 13:13
0

10% faster compared to the solution of @AlainTiemblo, because it does not use an additional is_file check:

$files = array_filter(glob("/*", GLOB_MARK), function($path){ return $path[ strlen($path) - 1 ] != '/'; });

Instead it uses the inbuild GLOB_MARK flag to add a slash to each directory and by that we are able to remove those entries through array_filter() and an anonymous function.

Since PHP 7.1.0 supports Negative numeric indices you can use this instead, too:

$files = array_filter(glob("/*", GLOB_MARK), function($path){return $path[-1] != '/';});

No relevant speed gain, but it helps avoiding the vertical scrollbar in this post ^^

As array_filter() preserve the keys you should consider re-indexing the array with array_values() afterwards:

$files = array_values($files);
4
  • Nope! I thought it worked at first but it lists folders as well. Sep 8, 2019 at 13:07
  • @JensTörnell php.net says: GLOB_MARK "Adds a slash to each directory returned" So if you get folders this would be a bug in glob. Did you test glob("/*", GLOB_MARK) alone to verify your discovery? Or did you use $path[-1] with an too old PHP version?
    – mgutt
    Sep 9, 2019 at 8:39
  • Not compatible with Windows because "GLOB_MARK - Adds a slash (a backslash on Windows) to each directory returned" whereas the answer uses only '/' to check.
    – DrLightman
    Jan 24, 2021 at 10:54
  • Jens and DrL: Change '/' to DIRECTORY_SEPARATOR in both proposed answers in both proposed answers (PHP 7.0 and later vs PHP before 7.0) then this answer will work on all operating systems where directory separator is one character. I have always used the array_filter with 'is _file' as the second parameter and found it plenty fast for typical directories but if you have a large enough directory to glob, this solution does work and should be marginally faster.
    – Ted Cohen
    Feb 18, 2022 at 8:34
0

Invert regexp do the job.

preg_grep(
    ';^.*(\\\\|/)$;',
    glob("/*", GLOB_MARK),
    PREG_GREP_INVERT
);

\\\\ is for Windows backslash 🤯🔫

0
function glob_file_only($path){
    return array_filter(glob($path,GLOB_MARK),function($file){
        return substr($file,-1)!=DIRECTORY_SEPARATOR;
    });
}

This builds on the work of others that answered. It only touches the directory once instead of twice and it works for windows as well as linux.

-1

This worked for me. if this helps anyone.

for file_name in glob.glob('**/*', recursive=True):
    # we get all files and dirs
    if os.path.isfile(file_name):
        # we have only the files
1
  • The question asks for PHP, not Python.
    – Jason
    Feb 5 at 1:26
-10
$all = glob("/*.*");

this will list everything with a "." after the file name. so basically, all files.

1
  • 17
    Since when do files need to have an extension? And what makes you think that a directory cannot have a dot in its name? Apr 9, 2013 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.