Home  >  Article  >  php教程  >  PHP获取文件夹内所有文件包括子目录文件的姓名或路径

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

WBOY
WBOYOriginal
2016-06-06 19:43:08866browse

PHP获取文件夹内所有文件包括子目录文件的名称或路径 /* * new getFile($_dir[,$_emptyDir,$_fileType]);* @parma $_dir 是目录名称* @parma $_emptyDir 是否获取空文件夹,选填,默认不获取,true则获取空文件夹* @parma $_fileType 获取文件名称的类型,选

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

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

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

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