简体   繁体   English

PHP:使用glob()列出* .txt和目录?

[英]PHP: Using glob() to list *.txt and directories?

Instead of listing all the files and directories 而不是列出所有文件和目录

foreach(glob("/*") as $file) {
    ...
}

Is there a way to filter *.txt files and directories? 有没有办法过滤* .txt文件和目录? Because with this example 因为在这个例子中

foreach(glob("/*.txt") as $file) {
    ...
}

Will only get the *.txt files, but not the directories. 将仅获取* .txt文件,而不获取目录。 But to list only directories, this will work: 但是只列出目录,这将起作用:

foreach(glob("/*", GLOB_ONLYDIR) as $file) {
    ...
}

but will left out the *.txt files. 但会忽略* .txt文件。 I am trying to avoid using preg_match() or similar operations post reading the directory because it will waste server resources if there are around 5000 unnecessary files (image files with *.jpg, *.png). 我试图避免在读取目录后使用preg_match()或类似的操作,因为如果有大约5000个不必要的文件(带有* .jpg,*。png的图像文件),它将浪费服务器资源。

Thank you. 谢谢。

Also tried GLOB_BRACE, but it didn't work, so had to use the following code: 还尝试了GLOB_BRACE,但没有成功,因此必须使用以下代码:

$aDir1 = glob('/*.txt');   // current directory
$aDir2 = glob('/*/*.txt'); // subdirectory, one level below

$aDir = asort(array_merge($aDir1, $aDir2));

Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM