Copy codeThe Code is as follows:
Function Files ($ path)
{
Foreach (scandir ($ path) as $ line)
{
If ($ line = '.' | $ line = '..') continue;
If (is_dir ($ path. '/'. $ line) Files ($ path. '/'. $ line );
Else echo '<li>'. $ path. '/'. $ line. '</li> ';
}
}
PHP traversal of files and folders
Add the specified folder C: \ Windows \ AppPatch
1. First, get everything under this folder, that is, files, folders, and put them in an array.
$ FileArr = array (
'Files '=> array (), // put an array in the file
'Dirs' => array (), // put an array in the folder
)
2. If a subfolder exists, traverse the subfolder, obtain the folder and file, and put it into the array, so that the loop does not leak
Copy codeThe Code is as follows:
<? Php
$ Dir = 'f: \ game ';
Function read_dir_all ($ dir ){
$ Ret = array ('dirs' => array (), 'files' => array ());
If ($ handle = opendir ($ dir )){
While (false! ==( $ File = readdir ($ handle ))){
If ($ file! = '.' & $ File! = '..'){
$ Cur_path = $ dir. DIRECTORY_SEPARATOR. $ file;
If (is_dir ($ cur_path )){
$ Ret ['dirs'] [$ cur_path] = read_dir_all ($ cur_path );
} Else {
$ Ret ['files'] [] = $ cur_path;
}
}
}
Closedir ($ handle );
}
Return $ ret;
}
$ P = read_dir_all ($ dir );
Echo '<pre> ';
Var_dump ($ p );
Echo '</pre> ';
?>
Php traverses all directories and files in a folder
During interviews, we often encounter this problem: php traverses all files and subfolders in a folder.
There are many solutions to this question. But the general idea is the same. Recursion is used.
Copy codeThe Code is as follows:
$ 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 );