Heim > php教程 > php手册 > Hauptteil

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

WBOY
Freigeben: 2016-06-06 19:43:08
Original
866 Leute haben es durchsucht

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>
Nach dem Login kopieren

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

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage