Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I searched before I ask, without lucky..

I looking for a simple script for myself, which I can search for files/folders. Found this code snippet in the php manual (I think I need this), but it is not work for me.

"Was looking for a simple way to search for a file/directory using a mask. Here is such a function.

By default, this function will keep in memory the scandir() result, to avoid scaning multiple time for the same directory."

<?php 
function sdir( $path='.', $mask='*', $nocache=0 ){ 
    static $dir = array(); // cache result in memory 
    if ( !isset($dir[$path]) || $nocache) { 
        $dir[$path] = scandir($path); 
    } 
    foreach ($dir[$path] as $i=>$entry) { 
        if ($entry!='.' && $entry!='..' && fnmatch($mask, $entry) ) { 
            $sdir[] = $entry; 
        } 
    } 
    return ($sdir); 
} 
?>

Thank you for any help,

Peter

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
682 views
Welcome To Ask or Share your Answers For Others

1 Answer

$a = new RegexIterator(
    new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator('DIRECTORY HERE')
    ),
    '/REGEX HERE/',
    RegexIterator::MATCH
);

foreach ($a as $v) {
    echo "$v
"; //$v will be the filename
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...