Home > Backend Development > PHP Tutorial > How to get directory list in php? How to recursively traverse files in a directory in php

How to get directory list in php? How to recursively traverse files in a directory in php

WBOY
Release: 2016-07-25 08:51:23
Original
1277 people have browsed it
How to obtain a directory list in PHP, how to recursively traverse files in a specified directory, use directory functions opendir and readdir in PHP programming, and combine PHP recursive functions to implement directory and file traversal operations.

1. How to get directory list in php

php get directory list:

Copy code Code example: '."n"; } } closedir($handle); } } } ?>

2. How to recursively traverse files in a specified directory with php and count the number of files

php recursively traverses the files in the specified directory and counts the number of files:

Example:

Copy code Code example:

"; //Read the current directory file echo readdir($dir)."
"; //Read the upper-level directory file while($filename=readdir($dir)){ //To determine whether the path under $dirname is a directory $newfile=$dirname."/".$filename; //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 ?>



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template