php 有没有办法只 glob() 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14084378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:36:06  来源:igfitidea点击:

Is there a way to glob() only files?

phpglobdirectory-browsing

提问by Alain Tiemblo

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

我知道glob可以查找文件夹中的所有文件或仅查找所有目录:

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 optimizationsthat make the second browsing quicker).

运行良好,但读取目录两次(即使有一些优化可以使第二次浏览更快)。

回答by Alain Tiemblo

I finally found a solution :

我终于找到了解决方案:

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

But take care, array_filterwill preserve numeric keys : use array_valuesif you need to reindex the array.

但请注意,array_filter将保留数字键:如果需要重新索引数组,请使用array_values

回答by RafaSashi

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

您可以使用GLOB_BRACE将文档与已知文件扩展名列表进行匹配:

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

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

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

回答by mulcher

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!

{*}意味着所有的文件结尾,所以每一个文件,但没有文件夹!

回答by mgutt

10% faster compared to the solution of @AlainTiemblo :

与@AlainTiemblo 的解决方案相比,速度提高了 10%:

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

It uses GLOB_MARKto add a slash to each directory and by that we are able to remove those entries through array_filter()and an anonymous function.

它用于GLOB_MARK向每个目录添加一个斜杠,这样我们就可以通过array_filter()和匿名函数删除这些条目。

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

由于 PHP 7.1.0 支持负数字索引,因此您也可以使用它:

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

No relevant speed gain, but it helps avoiding the stackoverflow scrollbar ^^

没有相关的速度增益,但它有助于避免 stackoverflow 滚动条 ^^

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

作为array_filter()保留键,您应该考虑在之后使用array_values()重新索引数组:

$files = array_values($files);

回答by tits magee

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

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

这将列出带有“.”的所有内容。在文件名之后。所以基本上,所有文件。