tags:

views:

172

answers:

2

Hi!

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

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

foreach ($a as $v) {
    echo "$v\n"; //$v will be the filename
}
Artefacto
Thank for the answers, Artefacto-s solution is good for me, but! Is there similar solution without regex? Or what is the regex, when I only want to search in the filenames with/without masks?example: stackoverflow.zip and I want to search on "overflow", or "stack", or "stackoverflow"...Thank you very much!
Peter
For that you only need the regexes `overflow`, `stack` and `stackoverflow`. Those three would match "stackoverflow.zip". You can use regexes in that simple way, you just have to be careful to escape characters that have special meaning.
Artefacto
Soo simple!! Thank you very much!
Peter
@Artefacto: +1 nice and efficient way to do it. Little off the topic to this question but could you share your ideas to my question here: http://stackoverflow.com/questions/3356376/php-creating-extensible-cms-system
Sarfraz
+1  A: 

try using glob() http://us2.php.net/manual/en/function.glob.php

Geek Num 88