有没有办法只glob()文件? [英] Is there a way to glob() only files?

查看:108
本文介绍了有没有办法只glob()文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

我终于找到了解决方案:

I finally found a solution :

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

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

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

这篇关于有没有办法只glob()文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆