文件搜索使用php的目录 [英] File searching in a directory using php

查看:89
本文介绍了文件搜索使用php的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用PHP列出文件夹及其子文件夹中的所有文件 您可以使用SPL迭代器。特别是 RecursiveDirectoryIterator ,它将递归遍历给定的目录结构。



像这样:

  $ realpath = realpath('/ path / to / file'); 


$ fileObjects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($ realpath),
RecursiveIteratorIterator :: SELF_FIRST); ($ object-> getFilename()==='filename.png'){
$ b $ echo $ object-> getPathname();




$ b $ SELF_FIRST 迭代模式基本上告诉迭代器父项是先被放置的(父母先来)。

How i can list all the files in a folder and its sub folders using php

解决方案

You can make use of the SPL iterators. In particular the RecursiveDirectoryIterator which will recursively traverse through a given directory structure.

Like so:

$realpath = realpath('/path/to/file');


$fileObjects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($realpath),
                 RecursiveIteratorIterator::SELF_FIRST);

foreach($fileObjects as $key => $object){
    if($object->getFilename() === 'filename.png') {
        echo $object->getPathname();
    }
}

The SELF_FIRST iteration mode basically tells the iterator that the parent items are to be placed first (parents come first).

这篇关于文件搜索使用php的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆