search
Homephp教程php手册PHP递归遍历指定目录的文件并统计文件数量的方法

PHP递归遍历指定目录的文件并统计文件数量的方法

Jun 13, 2016 am 09:09 AM
phpdesignationquantitydocumentmethodofTable of contentsstatisticsrecursionTraverse

PHP递归遍历指定目录的文件并统计文件数量的方法

 这篇文章主要介绍了PHP递归遍历指定目录的文件并统计文件数量的方法,涉及php文件及目录操作的技巧,非常具有实用价值,需要的朋友可以参考下

 

 

本文实例讲述了PHP递归遍历指定目录的文件并统计文件数量的方法。分享给大家供大家参考。具体实现方法如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

//递归函数实现遍历指定文件下的目录与文件数量

function total($dirname,&$dirnum,&$filenum){

$dir=opendir($dirname);

echo readdir($dir)."
"; //读取当前目录文件

echo readdir($dir)."
"; //读取上级目录文件

while($filename=readdir($dir)){

//要判断的是$dirname下的路径是否是目录

$newfile=$dirname."/".$filename;

//is_dir()函数判断的是当前脚本的路径是不是目录

if(is_dir($newfile)){

//通过递归函数再遍历其子目录下的目录或文件

total($newfile,$dirnum,$filenum);

$dirnum++;

}else{

$filenum++;

}

}

closedir($dir);

}

$dirnum=0;

$filenum=0;

total("E:/AppServ/www/phpMyAdmin",$dirnum,$filenum);

echo "目录总数:".$dirnum."
";

echo "文件总数:".$filenum."
";

//遍历指定文件目录与文件数量结束

?>

希望本文所述对大家的php程序设计有所帮助。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment