Maison > développement back-end > tutoriel php > PHP获取文件夹内所有文件包括子目录文件的名称或路径,_PHP教程

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

WBOY
Libérer: 2016-07-13 16:53:50
original
1402 Les gens l'ont consulté

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

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1056841.htmlTechArticlePHP获取文件夹内所有文件包括子目录文件的名称或路径, /* * new getFile($_dir[,$_emptyDir,$_fileType]);* @parma $_dir 是目录名称* @parma $_emptyDir 是否获...
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
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal