Home >Backend Development >PHP Tutorial >PHP code for traversing a folder and all files under it

PHP code for traversing a folder and all files under it

WBOY
WBOYOriginal
2016-07-25 09:00:27788browse
PHP implements the code to traverse the current folder and all files and folders under it. It mainly uses recursion. Friends in need can refer to it.

The code is as follows:

<?php
 /**
  * 遍历文件夹下所有文件
  * site bbs.it-home.org
 */
 $path = './filepath';  
 function getfiles($path)  
 {  
     if(!is_dir($path)) return;  
    $handle  = opendir($path);  
    while( false !== ($file = readdir($handle)))  
    {  
        if($file != '.'  &&  $file!='..')  
        {  
            $path2= $path.'/'.$file;  
            if(is_dir($path2))  
            {  
                echo '  ';  
                echo $file;  
               getfiles($path2);  
            }else 
            {  
               echo ' ';  
                echo $file;  
            }  
        }  
    }  
}  
 
print_r( getfiles($path));  

echo ' <HR>';  
 
function getdir($path)  
{  
    if(!is_dir($path)) return;  
    $handle = dir($path);  
    while($file=$handle->read())  
    {  
        if($file!='.' && $file!='..')  
        {  
            $path2 = $path.'/'.$file;  
            if(is_dir($path2))  
            {  
                    echo $file."\t";  
                     getdir($path2);  
            }else 
            {  
                echo $file.'';  
            }  
        }  
    }  
}  
 getdir($path);  
 
 echo '  <HR>';  
 
 function get_dir_scandir($path){  
 
    $tree = array();  
    foreach(scandir($path) as $single){  
        if($single!='.' && $single!='..')  
        {  
            $path2 = $path.'/'.$single;  
            if(is_dir($path2))  
            {  
                echo  $single."  \r\n";  
                 get_dir_scandir($path2);  
            }else 
            {  
                echo $single."  \r\n";  
            }  
        }  
    }  
}  
get_dir_scandir($path);  
 
  echo '  <HR>';  
 
function get_dir_glob(){  
    $tree = array();  
    foreach(glob('./curl/*') as $single){  
        echo $single."  \r\n";  
    }  
}  
get_dir_glob();  
 
   echo '  <HR>';  
function myscandir($path)  
{  
    if(!is_dir($path))  return;  
    foreach(scandir($path) as $file)  
    {  
        if($file!='.'  && $file!='..')  
        {  
            $path2= $path.'/'.$file;  
            if(is_dir($path2))  
            {  
                echo $file;  
                myscandir($path2);  
            }else 
            {  
                echo $file.'  ';  
            }  
        }  
    }  
}  
 
myscandir($path);  
 
   echo '  <HR>';  
 
function myglob($path)  
{  
    $path_pattern = $path.'/*';  
    foreach(glob($path_pattern) as $file)  
    {  
            if(is_dir($file))  
            {  
                echo $file;  
                myscandir($file);  
            }else 
            {  
                echo $file.'  
';  
            }  
    }  
}  
 
myglob($path);
?>


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