PHP file traversal implementation code

Source: Internet
Author: User
Tags glob

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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.