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 '
'. $path. ' /'. $line. '';
}
}
PHP Traversal files and folders
Join a given folder C:\\windows\\apppatch
1. First get all the things under this folder, that is, files, folders, put an array inside
$FILEARR = Array (
' Files ' = = Array (),//file put an array
' dirs ' = + Array (),//folder to put an array
)
2. If there are subfolders, traverse subfolders, get folders and files, also put into that array, so loop, one does not leak
Copy the Code code as follows:
$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 '
Echo '
';
?>
PHP iterates through all directories and files in a folder
We often encounter this topic during an interview: PHP iterates through all the files and subfolders in a folder.
There are many ways to solve this problem. But the general idea is the same. Recursion is used.
Copy the Code code as follows:
$path = './filepath ';
function GetFiles ($path)
{
if (!is_dir ($path)) return;
$handle = Opendir ($path);
while (false!== ($file = Readdir ($handle)))
{
if ($file! = '. ' && $file! = ' ... ')
{
$path 2= $path. ' /'. $file;
if (Is_dir ($path 2))
{
Echo ';
Echo $file;
GetFiles ($path 2);
}else
{
Echo ';
Echo $file;
}
}
}
}
Print_r (GetFiles ($path));
Echo ';
function Getdir ($path)
{
if (!is_dir ($path)) return;
$handle = Dir ($path);
while ($file = $handle->read ())
{
if ($file! = '. ' && $file! = ' ... ')
{
$path 2 = $path. ' /'. $file;
if (Is_dir ($path 2))
{
echo $file. " \ t ";
Getdir ($path 2);
}else
{
echo $file. ' ';
}
}
}
}
Getdir ($path);
Echo ';
function Get_dir_scandir ($path) {
$tree = Array ();
foreach (Scandir ($path) as $single) {
if ($single! = '. ' && $single! = ' ... ')
{
$path 2 = $path. ' /'. $single;
if (Is_dir ($path 2))
{
echo $single. " \ r \ n ";
Get_dir_scandir ($path 2);
}else
{
echo $single. " \ r \ n ";
}
}
}
}
Get_dir_scandir ($path);
Echo '
';
function Get_dir_glob () {
$tree = Array ();
foreach (Glob ('./curl/* ') as $single) {
echo $single. " \ r \ n ";
}
}
Get_dir_glob ();
Echo '
';
function Myscandir ($path)
{
if (!is_dir ($path)) return;
foreach (Scandir ($path) as $file)
{
if ($file! = '. ' && $file! = ' ... ')
{
$path 2= $path. ' /'. $file;
if (Is_dir ($path 2))
{
Echo $file;
Myscandir ($path 2);
}else
{
echo $file. ' ';
}
}
}
}
Myscandir ($path);
Echo ';
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);
The above describes the Pagefile.sys is what the file PHP traversal file implementation code, including the Pagefile.sys is what the content of the file, I hope the PHP tutorial interested in a friend helpful.