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

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

WBOY
WBOYoriginal
2016-06-13 08:54:141422parcourir

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

Déclaration:
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