程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php遍歷文件夾和文件列表示例分享

php遍歷文件夾和文件列表示例分享

編輯:PHP綜合

為PHP遍歷目錄和文件列表寫了一個簡單的類,並附上使用實例,大家參考使用吧

復制代碼 代碼如下:
<?php
define('DS', DIRECTORY_SEPARATOR);

class getDirFile{

    //返回數組
    private $DirArray  = array();
    private $FileArray = array();
    private $DirFileArray = array();

    private $Handle,$Dir,$File;

    //獲取目錄列表
    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;
    }

    //獲取文件列表
    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;
    }

    //獲取目錄/文件列表
    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;
    }

}
?>

實例:(相對路徑或絕對路徑)

1.獲取目錄列表
復制代碼 代碼如下:
<?php
$Dir_dir  = './example';
$getDirFile = new getDirFile();
$getDir = $getDirFile->getDir( $Dir_dir );
print_r($getDir);
?>

顯示
復制代碼 代碼如下:
<?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.獲取文件列表
復制代碼 代碼如下:
<?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);
?>

顯示
復制代碼 代碼如下:
Array
(
    [0] => example.sql
    [1] => example.txt
)

Array
(
    [0] => example.php
)

3.獲取目錄/文件列表
復制代碼 代碼如下:
<?php
$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
                )
        )
)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved