PHP recursive directory path

前端 未结 2 1335
野性不改
野性不改 2020-11-27 21:39

i have this function to return the full directory tree:

function getDirectory( $path = \'.\', $level = 0 ){

$ignore = array( \'cgi-bin\', \'.\'         


        
2条回答
  •  我在风中等你
    2020-11-27 22:16

    Try to use RecursiveIteratorIterator in combination with RecursiveDirectoryIterator

    $path = realpath('/path/you/want/to/search/in');
    
    $objects = new RecursiveIteratorIterator(
                   new RecursiveDirectoryIterator($path), 
                   RecursiveIteratorIterator::SELF_FIRST);
    
    foreach($objects as $name => $object){
        if($object->getFilename() === 'work.txt') {
            echo $object->getPathname();
        }
    }
    

    Additional reading:

    • http://www.phpro.org/tutorials/Introduction-to-SPL.html

提交回复
热议问题