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

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

WBOY
Libérer: 2016-06-06 19:43:08
original
918 Les gens l'ont consulté

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>
Copier après la connexion

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

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal