Home  >  Article  >  Backend Development  >  PHP traversal of folders and file lists example sharing_PHP tutorial

PHP traversal of folders and file lists example sharing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:36:14809browse

Written a simple class for PHP to traverse directories and file lists, and attached usage examples, please refer to it

Copy code The code is as follows:

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)) ; '.') ){
                                 $DirArray[] = $File;                                                             ( $Handle );
                                                                                                              ] = '[Path]:''.$Dir.'' is not a dir or not found!';
}
  return $DirArray;
}

//Get the file list
public function getFile( & $Dir ){
if( is_dir($Dir) ){
if( false != ($Handle = opendir($Dir)) ; .') ){
                                 $FileArray[] = $File;                     ( $Handle );
                                                                                                               = '[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 );
                   $ DirFileArray['FileList'][$Handle] = $this->getFile( $File ); '[Path]:' '.$Dir.'' is not a dir or not found!';
}
return $DirFileArray;
}

}
?>

Example: (relative path or absolute path)

1. Get the directory list



Copy the code

The code is as follows:


$Dir_dir = './example';
$getDirFile = new getDirFile();
$getDir = $getDirFile->getDir( $Dir_dir );

print_r($getDir);

?>

显示

复制代码 代码如下:

$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.获取文件列表

复制代码 代码如下:

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

显示

复制代码 代码如下:

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

Array
(
    [0] => example.php
)

3.获取目录/文件列表

复制代码 代码如下:

$Dir_dir  = './example';

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

print_r($getDirFile);
?>

显示

复制代码 代码如下:

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/740203.htmlTechArticle为PHP遍历目录和文件列表写了一个简单的类,并附上使用实例,大家参考使用吧 复制代码 代码如下: ?php define('DS', DIRECTORY_SEPARATOR); class g...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn