search
HomeBackend DevelopmentPHP TutorialHow to recursively traverse files in a specified directory and count the number of files in PHP_PHP Tutorial

How to recursively traverse files in a specified directory and count the number of files in PHP_PHP Tutorial

Jul 13, 2016 am 10:00 AM
phpdesignationquantitydocumentmethodofTable of contentsstatisticsrecursionTraverse

How to use PHP to recursively traverse files in a specified directory and count the number of files

This article mainly introduces how to use PHP to recursively traverse files in a specified directory and count the number of files, involving PHP File and directory operation skills are of great practical value. Friends who need them can refer to them

The example in this article describes how PHP recursively traverses files in a specified directory and counts the number of files. Share it with everyone for your reference. The specific implementation method is as follows:

?

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."
";

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

?>

1 2

3

4 5

67 8 9 10 11 12
13
14
15 16 17 18 19 20 21 22 23 24 25 26 27
//The recursive function implements traversing the directory and number of files under the specified file function total($dirname,&$dirnum,&$filenum){ $dir=opendir($dirname); echo readdir($dir)."
"; //Read the current directory file echo readdir($dir)."
"; //Read the upper-level directory file while($filename=readdir($dir)){ //What needs to be determined is whether the path under $dirname is a directory $newfile=$dirname."/".$filename; //The is_dir() function determines whether the path of the current script is a directory if(is_dir($newfile)){ //Traverse the directories or files in its subdirectories through the recursive function total($newfile,$dirnum,$filenum); $dirnum ; }else{ $filenum ; } } closedir($dir); } $dirnum=0; $filenum=0; total("E:/AppServ/www/phpMyAdmin",$dirnum,$filenum); echo "Total number of directories: ".$dirnum."
"; echo "Total number of files: ".$filenum."
"; //End of traversing the specified file directory and number of files ?>
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/973115.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/973115.htmlTechArticleHow to recursively traverse the files in the specified directory and count the number of files. This article mainly introduces the method of PHP recursively traversing the specified directory. The method of document and counting the number of files involves php files and objects...
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
How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

What is the maximum size of a file that can be uploaded using PHP ?What is the maximum size of a file that can be uploaded using PHP ?Apr 30, 2025 pm 03:35 PM

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

What is Nullable types in PHP ?What is Nullable types in PHP ?Apr 30, 2025 pm 03:34 PM

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec

See all articles

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor