代码之家  ›  专栏  ›  技术社区  ›  Warrior

使用php在目录中搜索文件

  •  0
  • Warrior  · 技术社区  · 15 年前

    如何使用PHP列出文件夹及其子文件夹中的所有文件

    4 回复  |  直到 15 年前
        1
  •  6
  •   Russell Dias    15 年前

    RecursiveDirectoryIterator

    $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();
        }
    }
    

    SELF_FIRST

        2
  •  0
  •   Explosion Pills    15 年前

    echo shell_exec('find -name *');
    

        3
  •  0
  •   Naveed    15 年前

    scandir

    <?php
        $dir    = '/tmp';
        $files = scandir($dir);
        print_r($files); 
    ?>
    

    Array
    (
        [0] => .
        [1] => ..
        [2] => bar.php
        [3] => foo.txt
        [4] => somedir
    )
    
        4
  •  0
  •   Blizz    15 年前