简体   繁体   English

如何使用PHP计算文件夹和子文件夹中的文件

[英]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.

相关问题 PHP CodeIgniter-如何计算文件夹及其各自的子文件夹中的图像 - PHP CodeIgniter - How to count images inside folders and their respective subfolders 递归返回文件夹子文件夹中除目录以外的所有文件的列表PHP - Return the list of all files excluding directory in the folders subfolders recursively PHP Google Drive PHP API:无法将文件或文件夹插入子文件夹 - Google drive PHP API: unable to insert files or folders into subfolders 使用php列出目录中的所有文件夹子文件夹和文件 - Listing all the folders subfolders and files in a directory using php php scandir首先对单个文件进行排序,然后对文件夹和子文件夹进行排序 - php scandir sort first single files then folders and subfolders 使用 PHP 列出文件夹和子文件夹中的图像文件并按创建日期排序 - Listing image files in folders and subfolders with PHP and sort them by date created 计算目录中的文件和子文件夹 - Count files and subfolders in directory 如何使用PHP计算文件夹中的文件? - How to count files inside a folder using PHP? 使用PHP删除FTP服务器中包含文件的文件夹 - Delete folders with files inside in FTP server with PHP 如何使用PHP创建文件夹和子文件夹,用户可以给出自己的名称和要创建的文件夹数 - How to create folders and subfolders using PHP where user can give his own NAME and number of folders to be created
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM