Home >Backend Development >PHP Tutorial >PHP recursively counts the number of folders and files

PHP recursively counts the number of folders and files

WBOY
WBOYOriginal
2016-07-29 09:16:091589browse

php recursively counts the number of folders and files

<?php
    header(&#39;Content-type:text/html;charset=utf8&#39;);
    /**
    * countDir() 递归统计文件夹数量和文件数量
    * @param $dirname 文件夹名
    * @return $arr 文件夹数量和文件数量
    */
    function countDir($dirname){
        global $dirnum,$filenum;
	if(!file_exists($dirname)){
	    return false;
	}
	$dir = opendir($dirname);
	readdir($dir);
	readdir($dir);
	while($filename = readdir($dir)){
	    $newfile = $dirname.&#39;/&#39;.$filename;
	    if(is_dir($newfile)){
	        countDir($newfile);
		$dirnum++;
	    }else{
	        $filenum++;
	    }
	}
	return array($dirnum,$filenum);
    }
    $a =  countDir(&#39;C:\wamp\www\erhaodian&#39;); 
    var_dump($a);
?>

Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces the PHP recursive statistics of the number of folders and files, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn