使用 PHP 返回文件夹内的文件总数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/224408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return total number of files within a folder using PHP
提问by PHLAK
Is there a better/simpler way to find the number of images in a directory and output them to a variable?
有没有更好/更简单的方法来查找目录中的图像数量并将它们输出到变量?
function dirCount($dir) {
$x = 0;
while (($file = readdir($dir)) !== false) {
if (isImage($file)) {$x = $x + 1}
}
return $x;
}
This seems like such a long way of doing this, is there no simpler way?
这似乎是一个很长的路要走,有没有更简单的方法?
Note:The isImage() function returns true if the file is an image.
注意:如果文件是图像,则 isImage() 函数返回 true。
回答by bbxbby
Check out the Standard PHP Library (aka SPL) for DirectoryIterator:
查看 DirectoryIterator 的标准 PHP 库(又名 SPL):
$dir = new DirectoryIterator('/path/to/dir');
foreach($dir as $file ){
$x += (isImage($file)) ? 1 : 0;
}
(FYI there is an undocumented function called iterator_count() but probably best not to rely on it for now I would imagine. And you'd need to filter out unseen stuff like . and .. anyway.)
(仅供参考,有一个名为 iterator_count() 的未记录函数,但我想现在最好不要依赖它。而且你需要过滤掉看不见的东西,比如 . 和 .. 无论如何。)
回答by rg88
This will give you the count of what is in your dir. I'll leave the part about counting only images to you as I am about to fallll aaasssllleeelppppppzzzzzzzzzzzzz.
这将为您提供目录中内容的数量。我将把关于只计算图像的部分留给你,因为我将要摔倒了。
iterator_count(new DirectoryIterator('path/to/dir/'));
回答by haheute
i do it like this:
我这样做:
$files = scandir($dir);
$x = count($files);
echo $x;
but it also counts the . and ..
但它也很重要。和 ..
回答by Josh Dunbar
The aforementioned code
上述代码
$count = count(glob("*.{jpg,png,gif,bmp}"));
is your best best, but the {jpg,png,gif} bit will only work if you append the GLOB_BRACEflag on the end:
是你最好的,但是 {jpg,png,gif} 位只有GLOB_BRACE在最后附加标志时才有效:
$count = count(glob("*.{jpg,png,gif,bmp}", GLOB_BRACE));
回答by nickf
you could use glob...
你可以用glob...
$count = 0;
foreach (glob("*.*") as $file) {
if (isImage($file)) ++$count;
}
or, I'm not sure how well this would suit your needs, but you could do this:
或者,我不确定这是否适合您的需求,但您可以这样做:
$count = count(glob("*.{jpg,png,gif,bmp}"));
回答by salathe
You could also make use of the SPL to filter the contents of a DirectoryIteratorusing your isImagefunction by extending the abstract FilterIteratorclass.
您还可以使用 SPL通过扩展抽象类来过滤DirectoryIterator使用您的isImage函数的内容FilterIterator。
class ImageIterator extends FilterIterator {
public function __construct($path)
{
parent::__construct(new DirectoryIterator($path));
}
public function accept()
{
return isImage($this->getInnerIterator());
}
}
You could then use iterator_count(or implement the Countableinterface and use the native countfunction) to determine the number of images. For example:
然后您可以使用iterator_count(或实现Countable接口并使用本机count函数)来确定图像的数量。例如:
$images = new ImageIterator('/path/to/images');
printf('Found %d images!', iterator_count($images));
Using this approach, depending on how you need to use this code, it might make more sense to move the isImagefunction into the ImageIteratorclass to have everything neatly wrapped up in one place.
使用这种方法,根据您需要如何使用此代码,将isImage函数移动到ImageIterator类中以将所有内容整齐地包装在一个地方可能更有意义。
回答by Shailesh Ladumor
I use the following to get the count for all types of files in one directory in Laravel
我使用以下命令获取 Laravel 中一个目录中所有类型文件的计数
$dir = public_path('img/');
$files = glob($dir . '*.*');
if ( $files !== false )
{
$total_count = count( $files );
return $totalCount;
}
else
{
return 0;
}
回答by Chris Kloberdanz
Your answer seems about as simple as you can get it. I can't think of a shorter way to it in either PHP or Perl.
您的答案似乎很简单。我想不出在 PHP 或 Perl 中更短的方法。
You might be able to a system / exec command involving ls, wc, and grep if you are using Linux depending how complex isImage() is.
如果您使用的是 Linux,您可能能够使用涉及 ls、wc 和 grep 的 system/exec 命令,具体取决于 isImage() 的复杂程度。
Regardless, I think what you have is quite sufficient. You only have to write the function once.
无论如何,我认为你拥有的已经足够了。您只需编写一次该函数。
回答by Marc
I use this to return a count of ALL files in a directory except . and ..
我用它来返回目录中所有文件的计数,除了 . 和 ..
return count(glob("/path/to/file/[!\.]*"));
Here is a good list of glob filtersfor file matching purposes.
这是用于文件匹配目的的 glob 过滤器的良好列表。
回答by user2444847
$nfiles = glob("/path/to/file/[!\.]*");
if ($nfiles !== FALSE){
return count($nfiles);
} else {
return 0;
}

