是否有一种方法可以只使用glob()文件?

时间:2022-09-01 23:09:22

I know that glob can 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 optimizations that make the second browsing quicker).

工作得很好,但是读取目录两次(即使有一些优化使第二次浏览更快)。

5 个解决方案

#1


45  

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.

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

#2


5  

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

您可以使用glob_将文档与已知文件扩展列表相匹配:

$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/

#3


4  

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!

{*}表示所有文件结尾,即每个文件,但没有文件夹!

#4


1  

Other solution:

其他解决方案:

$files = glob('../{,.}*', GLOB_BRACE);
foreach ($files as $i => $file) {
    if (is_dir($file)) {
        unset($files[$i]);
    }
}

Or:

或者:

$files = glob('../{,.}*', GLOB_BRACE | GLOB_MARK);
foreach ($files as $i => $file) {
    if ($file[ strlen($file) - 1 ] == '/') {
        unset($files[$i]);
    }
}

Both preserve numeric keys so you maybe want to re-index.

两者都保存数字键,所以您可能想要重新索引。

P.S. You must use{,.}*', GLOB_BRACE if you want to get hidden files, too.

注:您必须使用{。如果你想要隐藏文件,也可以使用glob_backup。

#5


-10  

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

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

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

#1


45  

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.

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

#2


5  

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

您可以使用glob_将文档与已知文件扩展列表相匹配:

$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/

#3


4  

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!

{*}表示所有文件结尾,即每个文件,但没有文件夹!

#4


1  

Other solution:

其他解决方案:

$files = glob('../{,.}*', GLOB_BRACE);
foreach ($files as $i => $file) {
    if (is_dir($file)) {
        unset($files[$i]);
    }
}

Or:

或者:

$files = glob('../{,.}*', GLOB_BRACE | GLOB_MARK);
foreach ($files as $i => $file) {
    if ($file[ strlen($file) - 1 ] == '/') {
        unset($files[$i]);
    }
}

Both preserve numeric keys so you maybe want to re-index.

两者都保存数字键,所以您可能想要重新索引。

P.S. You must use{,.}*', GLOB_BRACE if you want to get hidden files, too.

注:您必须使用{。如果你想要隐藏文件,也可以使用glob_backup。

#5


-10  

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

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

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