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

时间: 2024-08-04 05:34:14

php遍历文件夹和文件列表示例分享_php实例的相关文章

php遍历目录与文件夹的多种方法详解_php实例

遍历目录或遍历目录下指定类型的文件,这是每一个童鞋在写程序的时候难免会用到的.PHP本身也提供了很多灰常有用的函数,正确地使用它们,不会有错滴.下面就我个人学习过程中的一些总结,希望对想学PHP的童鞋有所帮助.本函数可以列出指定目录下所有的文件(包括子目录下的) 复制代码 代码如下: function getfiles($path){ foreach(scandir($path) as $afile){if($afile=='.'||$afile=='..') continue; if(is_d

php无限遍历文件夹示例分享_php实例

最近在能php目录操作,搞了一个目录无限遍历: 使用的函数有: isset()判断某个变量是否定义 chdir() 将当前目录改变为指定的目录. opendi()打开目录. readdir()读取目录. getcwd()获取当前目录. 还用到了for  if  GET传值 大概就这些东东: 下面是代码: 复制代码 代码如下: <?phpif(isset($_GET['id']))//判断是否传值{    $s=str_replace(' ','+',$_GET['id']);    $s=bas

PHP文件缓存类示例分享_php实例

复制代码 代码如下: <?php     /**      * @desc 文件缓存      */     class Cache{         const C_FILE = '/Runtime/';         private $dir = '';         const EXT = '.tpl';         private $filename = '';         public function __construct($dir = ''){            

php多文件上传下载示例分享_php实例

复制代码 代码如下: <html><head>    <meta charset="utf-8">    <title>index_uploads</title></head><body>    <form action="uploads.php" method="post" enctype="multipart/form-data"&g

php使用curl访问https示例分享_php实例

为方便说明,先上代码吧 复制代码 代码如下: /**  * curl POST  *  * @param   string  url  * @param   array   数据  * @param   int     请求超时时间  * @param   bool    HTTPS时是否进行严格认证  * @return  string  */  function curlPost($url, $data = array(), $timeout = 30, $CA = true){      

美图秀秀web开放平台--PHP流式上传和表单上传示例分享_php实例

废话少说,直接上代码: <?php /** * Note:for octet-stream upload * 这个是流式上传PHP文件 * Please be amended accordingly based on the actual situation */ $post_input = 'php://input'; $save_path = dirname(__FILE__); $postdata = file_get_contents($post_input); if (isset($p

php去除字符串换行符示例分享_php实例

第1种写法: 复制代码 代码如下: <?phpstr_replace("n", '', $str); ?> 第2种写法: 复制代码 代码如下: <?phpstr_replace("rn", '', $str); ?> 第3种写法: 复制代码 代码如下: <?phppreg_replace("/s/", '', $str); ?> 下面进行相关说明:首先说说 n,r,tn 软回车:在Windows中表示换行且回到

zf框架db类的分页示例分享_php实例

zf框架的分页示例 复制代码 代码如下: <?phpisset($_GET['page']) ? $page = $_GET['page'] : $page = 1;//引入Loader类(自动加载类)require_once("Zend/Loader.php");//使用Loader类引入一个Db类Zend_Loader::loadClass("Zend_Db");//引入Zend_Db的状态器Zend_Loader::loadClass("Zen

php实现水仙花数的4个示例分享_php实例

示例1: 复制代码 代码如下: <?phpfor($q=1;$q<=9;$q++){    for($w=0;$w<=9;$w++){      for($e=0;$e<=9;$e++){        if($q*$q*$q + $w*$w*$w + $e*$e*$e ==         100*$q + 10*$w + $e){           echo "$q $w $e "."<p>";        }