Copy Code code 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 traverse files and folders
Join a given folder C:\\windows\\apppatch
1. First get all the things under this folder, which is file, folder, put an array inside
$FILEARR = Array (
' Files ' => array (),//file put an array
' Dirs ' => Array (),//folder put an array
)
2. If there are subfolders, traverse subfolders, get folders and files, also put in that array, so loop, one does not leak
Copy Code code 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
In the interview we often encounter this topic: PHP Traverse a folder of all the files and subfolders.
There are many ways to solve this problem. But the general idea is the same. Adopt recursion.
Copy 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 ' <HR> ';
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 ' <HR> ';
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 '
<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!= ' ... ')
{
$path 2= $path. ' /'. $file;
if (Is_dir ($path 2))
{
Echo $file;
Myscandir ($path 2);
}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);