PHP:获取目录中文件数的最有效方法(PHP: Most efficient way to get the number of files within a directory)

编程入门 行业动态 更新时间:2024-10-27 19:27:07
PHP:获取目录中文件数的最有效方法(PHP: Most efficient way to get the number of files within a directory)

考虑这两个文件夹结构:

Foo/ Folder1/ File1.txt Folder2/ Folder3/ File2.txt Bar/ Folder1/ Folder2/ Folder3/ Folder4/

我想知道PHP中最有效的方式,告诉我“Foo”文件夹中有两个文件,“Bar”文件夹中有零个文件。 注意它是递归的。 即使“File1.txt”文件不是立即在“Foo”文件夹中,我仍然希望它被计数。 另外,我不在乎这些文件的名称是什么。 我只想要文件的总数。

任何帮助,将不胜感激。 谢谢!

Consider these two folder structures:

Foo/ Folder1/ File1.txt Folder2/ Folder3/ File2.txt Bar/ Folder1/ Folder2/ Folder3/ Folder4/

I'd like to know the most efficient way in PHP to tell me that the "Foo" folder has two files in it and that the "Bar" folder has zero files in it. Notice that it's recursive. Even though the "File1.txt" file is not immediately inside the "Foo" folder, I still want it to count. Also, I don't care what the names of the files are. I just want the total number of files.

Any help would be appreciated. Thanks!

最满意答案

使用RecursiveDirectoryIterator 。 这里是文档 。

$rdi = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/home/thrustmaster/Temp', FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::LEAVES_ONLY); foreach ($rdi as $file) echo "$file\n"; print iterator_count($rdi);

Use RecursiveDirectoryIterator. Here is the documentation.

$rdi = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/home/thrustmaster/Temp', FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::LEAVES_ONLY); foreach ($rdi as $file) echo "$file\n"; print iterator_count($rdi);

更多推荐

本文发布于:2023-08-02 07:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1372154.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:件数   最有效   中文   目录   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!