PHP Traversal folder and file list examples Share _php tutorial

Source: Internet
Author: User
Tags php define
For PHP to traverse the directory and file list to write a simple class, and attach the use of examples, we refer to the use of it

Copy the Code code as follows:
Define (' DS ', directory_separator);

Class getdirfile{

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

Get a list of files
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;
}

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

}
?>

Instance: (relative path or absolute path)

1. Get a list of directories
Copy the Code code as follows:
$Dir _dir = './example ';
$getDirFile = new Getdirfile ();
$getDir = $getDirFile->getdir ($Dir _dir);
Print_r ($getDir);
?>

Show
Copy the Code code 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 a list of files
Copy the Code code 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);
?>

Show
Copy the Code code as follows:
Array
(
[0] = Example.sql
[1] = Example.txt
)

Array
(
[0] = example.php
)

3. Get a list of directories/files
Copy the Code code as follows:
$Dir _dir = './example ';

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

Print_r ($getDirFile);
?>

Show
Copy the 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
)
)
)

http://www.bkjia.com/PHPjc/740203.html www.bkjia.com true http://www.bkjia.com/PHPjc/740203.html techarticle for PHP to traverse the directory and file list to write a simple class, and attach the use of examples, we refer to the use of the copy 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.