Hướng dẫn php glob only files

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).

Deepak Rai

2,1133 gold badges19 silver badges34 bronze badges

asked Dec 29, 2012 at 17:53

Hướng dẫn php glob only files

Alain TiembloAlain Tiemblo

35k16 gold badges120 silver badges151 bronze badges

1

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.

Hướng dẫn php glob only files

Steve Tauber

8,9035 gold badges41 silver badges46 bronze badges

answered Dec 29, 2012 at 17:53

Hướng dẫn php glob only files

Alain TiembloAlain Tiemblo

35k16 gold badges120 silver badges151 bronze badges

1

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!

answered Jun 19, 2015 at 13:41

2

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);

answered Mar 23, 2017 at 12:53

Hướng dẫn php glob only files

mguttmgutt

5,4772 gold badges49 silver badges73 bronze badges

4

Invert regexp do the job.

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

\\\\ is for Windows backslash 🤯🔫

answered Oct 5, 2021 at 19:54

Hướng dẫn php glob only files

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.

Hướng dẫn php glob only files

Suraj Rao

29.1k11 gold badges95 silver badges100 bronze badges

answered Jul 1 at 10:18

Ted CohenTed Cohen

9889 silver badges16 bronze badges

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

answered Nov 30, 2021 at 12:55

code-freezecode-freeze

4356 silver badges7 bronze badges

$all = glob("/*.*");

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

answered Apr 9, 2013 at 21:54

1

Not the answer you're looking for? Browse other questions tagged php glob directory-browsing or ask your own question.