PHP Traversal folder and file list example share _php instance

Source: Internet
Author: User

For PHP traversal directory and file list wrote a simple class, and attached to the use of examples, we refer to the use of the bar

Copy Code code as follows:

<?php
Define (' DS ', directory_separator);

Class getdirfile{

Return array
Private $DirArray = Array ();
Private $FileArray = Array ();
Private $DirFileArray = Array ();

Private $Handle, $Dir, $File;

Get 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 ' found! ';
}
return $DirArray;
}

Get 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 ' found! ';
}
return $FileArray;
}

Get 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 ' found! ';
}
return $DirFileArray;
}

}
?>

Instance: (relative path or absolute path)

1. Get directory List

Copy Code code as follows:

<?php
$Dir _dir = './example ';
$getDirFile = new Getdirfile ();
$getDir = $getDirFile->getdir ($Dir _dir);
Print_r ($getDir);
?>

Show

Copy Code code as follows:

<?php
$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 file List

Copy Code code as follows:

<?php
$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);
?>

Show

Copy Code code as follows:

Array
(
[0] => Example.sql
[1] => Example.txt
)

Array
(
[0] => example.php
)

3. Get Directory/File list

Copy Code code as follows:

<?php
$Dir _dir = './example ';

$getDirFile = new Getdirfile ();
$getDirFile = $getDirFile->getdirfile ($Dir _dir);

Print_r ($getDirFile);
?>

Show

Copy Code code 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
)
)
)

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.