Rumah > php教程 > php手册 > teks badan

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

WBOY
Lepaskan: 2016-06-13 08:54:14
asal
1425 orang telah melayarinya

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>
Salin selepas log masuk

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

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan