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);更多推荐
发布评论