首頁 > php教程 > php手册 > 主體

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

WBOY
發布: 2016-06-13 08:54:14
原創
1422 人瀏覽過

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

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板