【发布时间】:2015-12-06 13:54:34
【问题描述】:
如何使用php统计服务器中所有文件夹、文件和子文件夹中的文件?
我想计算路径/home1/example/public_html/中子文件夹中的所有文件、文件夹和文件
首先我使用这个代码
<?php
$directory = "/home1/example/public_html/";
$filecount = 0;
$files = glob($directory . "*");
if ($files){
$filecount = count($files);
}
echo "There were $filecount files";
?>
节目There were 561 files
然后我使用这个代码
<?php
$fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
printf("There were %d Files", iterator_count($fi));
?>
节目There were 566 Files
最后我使用了这段代码
<?php
// integer starts at 0 before counting
$i = 0;
$dir = '/home1/example/public_html/';
if ($handle = opendir($dir)) {
while (($file = readdir($handle)) !== false){
if (!in_array($file, array('.', '..')) && !is_dir($dir.$file))
$i++;
}
}
// prints out how many were in the directory
echo "There were $i files";
?>`
节目There were 500 Files
但结果不同。
我通过在/home1/example/public_html/images/ 中创建文件进行了测试,但所有结果仍然显示与我创建文件之前相同。
我该怎么办?
【问题讨论】: