【问题标题】:how to count how many sub-directories a file is in?如何计算一个文件有多少个子目录?
【发布时间】:2012-04-01 07:20:00
【问题描述】:

我有一个名为 count.php 的文件。

这是count.php 的存储位置(绝对路径):

/home/content/61/7475161/html/wordpress/project/folder1/folder2/folder3/folder4/count.php

我需要一种方法来计算路径中涉及文件夹 project 的子文件夹的数量。

所以在上面的例子中,它是四个。我想出了一种方法:

$pathtofile=dirname(__FILE__);

$path_array=explode("/",$pathtofile);

$f_stat=false;
$num_folders=0;

foreach ($path_array as $folder){

if($f_stat===false){

if($folder=="project"){$f_stat=true}

}//end of f_stat false

else{
$num_folders++;
}//end of else

}//end of foreach
//now $num_folders will equal 4 xD

我认为这不是最好的方法,所以任何建议都会很棒。谢谢!

【问题讨论】:

    标签: php path directory subdirectory


    【解决方案1】:
     $dir=glob(directoryname."/*",GLOB_ONLYDIR);
    

    $dir return an array 包含文件夹内的所有子文件夹。

    【讨论】:

    • 辛苦了。是否有可能进一步阐述这一点?
    【解决方案2】:

    只需使用/project 执行strstr 并计算爆炸后的数组元素,如下所示:

    $path_array=explode('/',strstr($pathtofile, '/project'));
    $depth = count($path_array)-1;
    

    【讨论】:

      【解决方案3】:
      echo (substr_count('/',$path) - 7); //> Subtract the number of main dir
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-04
        • 1970-01-01
        • 2014-12-06
        • 2021-08-21
        相关资源
        最近更新 更多