Home > Backend Development > PHP Tutorial > In-depth analysis of the method of traversing all files and folders in PHP_PHP Tutorial

In-depth analysis of the method of traversing all files and folders in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:08:26
Original
958 people have browsed it

1. Method 1:

Copy the code The code is as follows:

< ;?
$dir="D:";
static $dir_list =0;
static $file_list =0;
function listfile($dir){
global $dir_list,$file_list ;
$d = dir($dir);
while ( $entry = $d->read()) {
$tem_curnt=$dir."/".$entry;
if($entry=="." || $entry=="..") continue;
if ( is_dir( $tem_curnt)) {
listfile($tem_curnt);
echo "folder ".$tem_curnt."
";
$dir_list++;
}
elseif ( is_file($tem_curnt)) {
echo "file".$tem_curnt."
";
$file_list++;
}
}
$d->close();
}
listfile($dir);
echo "Number of directories:" .$dir_list;
echo"
";
echo" Number of files: ".$file_list;
?>

2 .Method 2:
Copy code The code is as follows:

function list_dir($dirpath){
if($dirpath[strlen($dirpath)]-1!='/'){
$dirpath.="/";
}
static $result_array =array();
if(is_dir($dirpath)){
$files_dirs=scandir($dirpath);
foreach($files_dirs as $file){
if($file== '.'||$file=='..'){continue;}
if(is_dir($dirpath.$file)){
list_dir($dirpath.$file);
}else {
array_push($result_array,$dirpath.$file);
}
}
}
return $result_array;
}
$array=list_dir('D: /www');
foreach($array as $value){
echo $value;
echo "
";
}
?>

3. Method three:
Copy the code The code is as follows:

function list_dir($dirpath){
if($dirpath[strlen($dirpath)-1]!="//"){$dirpath.="//"; }
static $result_array=array();
if(is_dir($dirpath)){
$handle=opendir($dirpath);
while($file=readdir($handle)) {
if($file=="."||$file==".."){continue;}
if(is_dir($dirpath.$file)){
list_dir($dirpath .$file."//");
}else{
array_push($result_array,$dirpath.$file);
}
}
closedir($handle);
}
return $result_array;
}
$array=list_dir("D:/www");
foreach($array as $value){
echo $value;
echo "
";
}
?>

4. Method 4:
Copy code The code is as follows:

function list_dir($dirpath){
if($dirpath[strlen ($dirpath)-1]!="//"){$dirpath.="//";}
static $result_array=array();
if(is_dir($dirpath)){
$dir=dir($dirpath);
while($file=$dir->read()){
if($file=="."||$file==".." ){continue;}
if(is_dir($dirpath.$file)){
list_dir($dirpath.$file."//");
}else{
array_push($result_array ,$dirpath.$file);
}
}
$dir->close();
}
return $result_array;
}
$array=list_dir ("D:/www");
foreach($array as $value){
echo $value;
echo "
";
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327479.htmlTechArticle1. Method 1: Copy the code as follows: ? $dir="D:"; static $dir_list =0 ; static $file_list =0; function listfile($dir){ global $dir_list,$file_list; $d = dir($dir); while ( $ent...
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