php 递归计数文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 递归计数文件相关的知识,希望对你有一定的参考价值。
<span style="color: #000000; font-weight: bold;">function</span> count_files<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$path</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// (Ensure that the path contains an ending slash)</span>
<span style="color: #0000ff;">$file_count</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #0000ff;">$dir_handle</span> = <a href="http://www.php.net/opendir"><span style="color: #000066;">opendir</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$path</span><span style="color: #66cc66;">)</span>;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!<span style="color: #0000ff;">$dir_handle</span><span style="color: #66cc66;">)</span> <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">-1</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$file</span> = <a href="http://www.php.net/readdir"><span style="color: #000066;">readdir</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$dir_handle</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$file</span> == <span style="color: #ff0000;">'.'</span> || <span style="color: #0000ff;">$file</span> == <span style="color: #ff0000;">'..'</span><span style="color: #66cc66;">)</span> <span style="color: #b1b100;">continue</span>;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><a href="http://www.php.net/is_dir"><span style="color: #000066;">is_dir</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$path</span> . <span style="color: #0000ff;">$file</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #0000ff;">$file_count</span> += count_files<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$path</span> . <span style="color: #0000ff;">$file</span> . DIRECTORY_SEPARATOR<span style="color: #66cc66;">)</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #b1b100;">else</span> <span style="color: #66cc66;">{</span>
<span style="color: #0000ff;">$file_count</span>++; <span style="color: #808080; font-style: italic;">// increase file count</span>
<span style="color: #66cc66;">}</span>
<span style="color: #66cc66;">}</span>
<a href="http://www.php.net/closedir"><span style="color: #000066;">closedir</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$dir_handle</span><span style="color: #66cc66;">)</span>;
<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$file_count</span>;
<span style="color: #66cc66;">}</span>
以上是关于php 递归计数文件的主要内容,如果未能解决你的问题,请参考以下文章