首页 > php教程 > php手册 > 正文

PHP获取文件夹内所有文件包括子目录文件的名称或路径,

WBOY
发布: 2016-06-13 08:54:14
原创
1425 人浏览过

PHP获取文件夹内所有文件包括子目录文件的名称或路径,

<span>/*</span><span>
*  new getFile($_dir[,$_emptyDir,$_fileType]);
* @parma $_dir 是目录名称
* @parma $_emptyDir 是否获取空文件夹,选填,默认不获取,true则获取空文件夹
* @parma $_fileType 获取文件名称的类型,选填,默认获取只文件名称,true则获取带有路径的文件名称
*</span><span>*/</span>
<span>class</span><span> getFiles {
    </span><span>private</span> <span>$_dir</span><span>;
    </span><span>private</span> <span>$_emptyDir</span><span>;
    </span><span>private</span> <span>$_fileType</span><span>;
    </span><span>public</span> <span>$_files</span><span>;
     
    </span><span>public</span> <span>function</span> __construct(<span>$_dir</span>,<span>$_emptyDir</span> = <span>false</span>,<span>$_fileType</span> = <span>false</span><span>) {
        </span><span>$this</span>->_dir = <span>$_dir</span><span>;
        </span><span>$this</span>->_emptyDir = <span>$_emptyDir</span><span>;
        </span><span>$this</span>->_fileType = <span>$_fileType</span><span>;
        </span><span>if</span> (<span>$this</span>-><span>_emptyDir) {
            </span><span>$this</span>->getFileEmpty(<span>$this</span>->_dir,<span>$this</span>-><span>_files);
        } </span><span>else</span><span> {
            </span><span>$this</span>->getFile(<span>$this</span>->_dir,<span>$this</span>-><span>_files);
        }
    }
    </span><span>//</span><span>读取文件夹所有文件不包括空文件夹</span>
    <span>private</span> <span>function</span> getFile(<span>$_dir</span>,&<span>$_arr</span><span>) {
        </span><span>if</span> (!!<span>$_dirs</span> = <span>opendir</span>(<span>$_dir</span><span>)) {
            </span><span>while</span> ((<span>$_file</span>=<span>readdir</span>(<span>$_dirs</span>)) != <span>false</span><span>) {
                </span><span>if</span> (<span>$_file</span> == '.' || <span>$_file</span> == '..') <span>continue</span><span>;
                </span><span>$_files</span> = <span>$_dir</span> .'/'. <span>$_file</span><span>;
                </span><span>if</span> (<span>is_dir</span>(<span>$_files</span><span>)) {
                    </span><span>$this</span>->getFile(<span>$_files</span>,<span>$_arr</span><span>);
                } </span><span>else</span><span> {
                    </span><span>if</span> (<span>$this</span>-><span>_fileType) {
                        </span><span>$_arr</span>[] = <span>$_files</span><span>;
                    } </span><span>else</span><span> {
                        </span><span>$_arr</span>[] = <span>$_file</span><span>;
                    }
                }
            }
        }
        </span><span>closedir</span>(<span>$_dirs</span><span>);
    }
    </span><span>//</span><span>读取文件夹所有文件包括空文件夹</span>
    <span>private</span> <span>function</span> getFileEmpty(<span>$_dir</span>,&<span>$_arr</span><span>) {
        </span><span>if</span> (!!<span>$_dirs</span> = <span>opendir</span>(<span>$_dir</span><span>)) {
            </span><span>while</span> ((<span>$_file</span>=<span>readdir</span>(<span>$_dirs</span>)) != <span>false</span><span>) {
                </span><span>if</span> (<span>$_file</span> == '.' || <span>$_file</span> == '..') <span>continue</span><span>;
                </span><span>$_files</span> = <span>$_dir</span> .'/'. <span>$_file</span><span>;
                </span><span>if</span> (<span>is_dir</span>(<span>$_files</span>) && <span>$this</span>->isEmpty(<span>$_files</span><span>)) {
                    </span><span>$this</span>->getFileEmpty(<span>$_files</span>,<span>$_arr</span><span>);
                } </span><span>else</span><span> {
                    </span><span>if</span> (<span>$this</span>-><span>_fileType) {
                        </span><span>$_arr</span>[] = <span>$_files</span><span>;
                    } </span><span>else</span><span> {
                        </span><span>$_arr</span>[] = <span>$_file</span><span>;
                    }
                }
            }
        }
        </span><span>closedir</span>(<span>$_dirs</span><span>);
    }
    </span><span>//</span><span>判断文件夹是否为空</span>
    <span>private</span> <span>function</span> isEmpty(<span>$_dir</span><span>) {
        </span><span>if</span> (!!<span>$_dirs</span> = <span>opendir</span>(<span>$_dir</span><span>)) {
            </span><span>while</span> ((<span>$_file</span>=<span>readdir</span>(<span>$_dirs</span>)) != <span>false</span><span>) {
                </span><span>if</span>(<span>$_file</span> != '.' && <span>$_file</span> != '..'<span>) {
                    </span><span>closedir</span>(<span>$_dirs</span><span>);
                    </span><span>return</span> <span>true</span><span>;
                    </span><span>break</span><span>;
                }
            }
            </span><span>closedir</span>(<span>$_dirs</span><span>);
            </span><span>return</span> <span>false</span><span>;
        }
    }
}</span>
登录后复制

转载请注明出处:http://www.ly89.cn/detailB/36.html

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板