是否有一种有效的方法来计算单个文件夹中的文件,其中包含更多文件和子文件夹?
我试过这样的事情,仍然没有给我正确的号码。 Windows识别Images文件夹中有159个文件。当我只得到144
<?php
$img = count(glob("./assets/images/*.*"));
$about = count(glob("./assets/images/aboutus/*.*"));
$blog1 = count(glob("./assets/images/blog/*.*"));
$mason = count(glob("./assets/images/blog/masonary/*.*"));
$tl = count(glob("./assets/images/blog/timeline/*.*"));
$blog2 = count(glob("./assets/images/blogdetails/*.*"));
$gallery = count(glob("./assets/images/gallery/*.*"));
$home = count(glob("./assets/images/home/*.*"));
$keg = count(glob("./assets/images/kegiatan/*.*"));
$port1 = count(glob("./assets/images/portfolio/*.*"));
$port2 = count(glob("./assets/images/portfolio-details/*.*"));
$srv = count(glob("./assets/images/services/*.*"));
$usr = count(glob("./assets/images/users/*.*"));
$count = $img+$about+$blog1+$mason+$tl+$blog2+$gallery+$home+$keg+$port1+$port2+$srv+$usr;
?>
帮助??
EDITED: 完成。我的错。我没有计算某个文件夹中的子文件夹。
但是,有没有办法让它缩短?
答案 0 :(得分:0)
这是你可以尝试的一个开始。您仍然需要修改它来计算文件数或计算给定的数组。
function getFromDir( $dir ) {
$cdir = scandir( $dir );
$result = array();
foreach( $cdir as $key => $value ) {
if( !in_array( $value, array('.', '..') ) ) {
if( is_dir( $dir . DIRECTORY_SEPARATOR . $value ) ) {
$result[$value] = getFromDir($dir . DIRECTORY_SEPARATOR . $value);
} else {
$result[] = $value;
}
}
}
return $result;
}
如果您将"./assets/images/"
作为参数传递,则应该获取所有子文件夹。
所以你最终得到的结果是:
$count = count(getFromDir("./assets/images/"));