> php教程 > php手册 > 본문

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

WBOY
풀어 주다: 2016-06-06 19:43:08
원래의
866명이 탐색했습니다.

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

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿