计算PHP目录中有多少文件 [英] Count how many files in directory PHP
本文介绍了计算PHP目录中有多少文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在做一个稍微新的项目.我想知道某个目录中有多少个文件.
<?php$dir = opendir('上传/');# 这是它将从中计数的目录$i = 0;# 整数从 0 开始计数# 虽然 false 不等于文件目录while (false !== ($file = readdir($dir))) {if (!in_array($file, array('.', '..') and !is_dir($file)) $i++;}echo "有 $i 个文件";# 打印出目录中有多少?>
这是我目前所拥有的(来自搜索).但是,它没有正确显示?我已经添加了一些注释,所以可以随意删除它们,它们只是为了让我尽可能地理解它.
如果您需要更多信息或觉得我描述得不够充分,请随时说明.
解决方案
您可以简单地执行以下操作:
$fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);printf("有 %d 个文件", iterator_count($fi));
I'm working on a slightly new project. I wanted to know how many files are in a certain directory.
<div id="header">
<?php
$dir = opendir('uploads/'); # This is the directory it will count from
$i = 0; # Integer starts at 0 before counting
# While false is not equal to the filedirectory
while (false !== ($file = readdir($dir))) {
if (!in_array($file, array('.', '..') and !is_dir($file)) $i++;
}
echo "There were $i files"; # Prints out how many were in the directory
?>
</div>
This is what I have so far (from searching). However, it is not appearing properly? I have added a few notes so feel free to remove them, they are just so I can understand it as best as I can.
If you require some more information or feel as if I haven't described this enough please feel free to state so.
解决方案
You can simply do the following :
$fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
printf("There were %d Files", iterator_count($fi));
这篇关于计算PHP目录中有多少文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文