[英]How to count files inside folders and subfolders with PHP
Is there a effective way to count files inside a single folder which inside it have more files and subfolders? 是否有一种有效的方法来计算单个文件夹中的文件,其中包含更多文件和子文件夹?
I tried something like this, still didn't give me the right number. 我试过这样的事情,仍然没有给我正确的号码。 Windows recognise there are 159 files inside Images folder.
Windows识别Images文件夹中有159个文件。 When I only get 144
当我只得到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;
?>
Help?? 救命??
EDITED: Done. 编辑:完成。 My mistakes.
我的错。 I didn't count the subfolders in certain folder.
我没有计算某个文件夹中的子文件夹。
But still, is there a way to make it short? 但是,有没有办法让它缩短?
Here is a start of what you could try. 这是你可以尝试的一个开始。 You still need to modify it to count the files or count the array that is given.
您仍然需要修改它来计算文件或计算给定的数组。
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;
}
If you would pass "./assets/images/"
as a paramater it should get all sub folders aswell. 如果您将
"./assets/images/"
作为参数传递,它应该获得所有子文件夹。
So you end up with something like: 所以你最终得到的结果如下:
$count = count(getFromDir("./assets/images/"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.