Php traversal folder and file list example sharing _ PHP Tutorial

Source: Internet
Author: User
Tags php define
Php traversal folder and file list example sharing. I wrote a simple class for PHP to traverse the directory and file list, and attached the example for use. for details, refer to the following code to copy the code :? Phpdefine (DS, DIRECTORY_SEPARATOR); classg writes a simple class for the PHP directory traversal and file list, and attaches the example for your reference.

The code is as follows:


Define ('Ds', DIRECTORY_SEPARATOR );

Class getDirFile {

// Returns an array.
Private $ DirArray = array ();
Private $ FileArray = array ();
Private $ DirFileArray = array ();

Private $ Handle, $ Dir, $ File;

// Obtain the directory list
Public function getDir (& $ Dir ){
If (is_dir ($ Dir )){
If (false! = ($ Handle = opendir ($ Dir ))){
While (false! = ($ File = readdir ($ Handle ))){
If ($ File! = '.' & $ File! = '..'&&! Strpos ($ File ,'.')){
$ DirArray [] = $ File;
}
}
Closedir ($ Handle );
}
} Else {
$ DirArray [] = '[Path]: \ ''. $ Dir.' \ 'is not a dir or not found! ';
}
Return $ DirArray;
}

// Obtain the file list
Public function getFile (& $ Dir ){
If (is_dir ($ Dir )){
If (false! = ($ Handle = opendir ($ Dir ))){
While (false! = ($ File = readdir ($ Handle ))){
If ($ File! = '.' & $ File! = '..' & Strpos ($ File ,'.')){
$ FileArray [] = $ File;
}
}
Closedir ($ Handle );
}
} Else {
$ FileArray [] = '[Path]: \ ''. $ Dir.' \ 'is not a dir or not found! ';
}
Return $ FileArray;
}

// Obtain the directory/file list
Public function getDirFile (& $ Dir ){
If (is_dir ($ Dir )){
$ DirFileArray ['dirlist'] = $ this-> getDir ($ Dir );
If ($ DirFileArray ){
Foreach ($ DirFileArray ['dirlist'] as $ Handle ){
$ File = $ Dir. DS. $ Handle;
$ DirFileArray ['filelist'] [$ Handle] = $ this-> getFile ($ File );
}
}
} Else {
$ DirFileArray [] = '[Path]: \ ''. $ Dir.' \ 'is not a dir or not found! ';
}
Return $ DirFileArray;
}

}
?>

Instance: (relative or absolute path)

1. get the directory list

The code is as follows:


$ Dir_dir = './example ';
$ GetDirFile = new getDirFile ();
$ GetDir = $ getDirFile-> getDir ($ Dir_dir );
Print_r ($ getDir );
?>

Display

The code is as follows:


$ File_one_dir = './example/example_one ';
$ File_two_dir = 'E:/Workspace/mycode/getDirFile/example/example_two ';

$ GetDirFile = new getDirFile ();
$ GetFile_one = $ getDirFile-> getFile ($ File_one_dir );
$ GetFile_two = $ getDirFile-> getFile ($ File_two_dir );

Print_r ($ getFile_one );
Print_r ($ getFile_two );
?>

2. get the file list

The code is as follows:


$ File_one_dir = './example/example_one ';
$ File_two_dir = 'E:/Workspace/mycode/getDirFile/example/example_two ';

$ GetDirFile = new getDirFile ();
$ GetFile_one = $ getDirFile-> getFile ($ File_one_dir );
$ GetFile_two = $ getDirFile-> getFile ($ File_two_dir );

Print_r ($ getFile_one );
Print_r ($ getFile_two );
?>

Display

The code is as follows:


Array
(
[0] => example. SQL
[1] => example.txt
)

Array
(
[0] => example. php
)

3. get the directory/file list

The code is as follows:


$ Dir_dir = './example ';

$ GetDirFile = new getDirFile ();
$ GetDirFile = $ getDirFile-> getDirFile ($ Dir_dir );

Print_r ($ getDirFile );
?>

Display

The code is as follows:


Array
(
[DirList] => Array
(
[0] => example_one
[1] => example_two
)

[FileList] => Array
(
[Example_one] => Array
(
[0] => example. SQL
[1] => example.txt
)

[Example_two] => Array
(
[0] => example. php
)
)
)

The pipeline code is as follows :? Php define ('Ds', DIRECTORY_SEPARATOR); class g...

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.