使用 PHP 返回文件夹内的文件总数
有没有更好/更简单的方法来查找目录中的图像数量并将其输出到变量?
function dirCount($dir) {
$x = 0;
while (($file = readdir($dir)) !== false) {
if (isImage($file)) {$x = $x + 1}
}
return $x;
}
这看起来是一个很长的方法,有没有更简单的方法?
注意: 如果文件是图像,则 isImage() 函数返回 true。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
查看 DirectoryIterator 的标准 PHP 库(又名 SPL):(
仅供参考,有一个名为 iterator_count() 的未记录函数,但我想现在最好不要依赖它。并且您需要过滤掉像 .无论如何。)
Check out the Standard PHP Library (aka SPL) for DirectoryIterator:
(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.)
这将为您提供目录中内容的计数。 我将把只计算图像的部分留给你,因为我快要摔倒了。
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.
我这样做:
但它也计算 . 和 ..
i do it like this:
but it also counts the . and ..
上述代码
是最好的,但 {jpg,png,gif} 位只有在末尾附加
GLOB_BRACE
标志才有效:The aforementioned code
is your best best, but the {jpg,png,gif} bit will only work if you append the
GLOB_BRACE
flag on the end:你可以使用
glob
...或者,我不确定这是否能满足你的需求,但你可以这样做:
you could use
glob
...or, I'm not sure how well this would suit your needs, but you could do this:
您还可以通过扩展抽象
FilterIterator
类,利用isImage
函数利用 SPL 来过滤DirectoryIterator
的内容。然后,您可以使用
iterator_count
(或实现Countable
接口并使用本机count
函数)来确定图像的数量。 例如:使用这种方法,根据您需要如何使用此代码,将
isImage
函数移动到ImageIterator
类中以使所有内容整齐地包装可能更有意义放在一处。You could also make use of the SPL to filter the contents of a
DirectoryIterator
using yourisImage
function by extending the abstractFilterIterator
class.You could then use
iterator_count
(or implement theCountable
interface and use the nativecount
function) to determine the number of images. For example:Using this approach, depending on how you need to use this code, it might make more sense to move the
isImage
function into theImageIterator
class to have everything neatly wrapped up in one place.我使用以下命令来获取 Laravel 中一个目录中所有类型文件的计数
I use the following to get the count for all types of files in one directory in Laravel
您的答案似乎非常简单。 我想不出用 PHP 或 Perl 更短的方法。
如果您使用的是 Linux,您可能能够使用涉及 ls、wc 和 grep 的 system/exec 命令,具体取决于 isImage() 的复杂程度。
无论如何,我认为你所拥有的已经足够了。 您只需编写该函数一次。
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.
You might be able to a system / exec command involving ls, wc, and grep if you are using Linux depending how complex isImage() is.
Regardless, I think what you have is quite sufficient. You only have to write the function once.
我用它来返回目录中除 . 和..
这是用于文件匹配目的的一个很好的全局过滤器列表。
I use this to return a count of ALL files in a directory except . and ..
Here is a good list of glob filters for file matching purposes.