开发者

php scandir --> search for files/directories

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


$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
}


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


i you just want to search for a file, you can use this snippet:

    <?php   
    $s = $get['s'];
    $e = ".htm";
    $folders = array("data1", "data2", "data3");
    $files = array(); // nothing needed here. anything in this array will be showed as a search result.
    for($i=0;$i<=count($folders)-1;$i++) {
        $glob = glob($folders[$i]);
        $files = array_merge($files, $glob[$i]);
    }
    echo "Search - $s<br><br>";
    if(count($files) == 1) {
        echo "<li><a href='$files[0]'>".heir($files[0])."</a></li>";
    }
    if(count($files) != 1) {
        for($i=0;$i<=count($files)-1;$i++) {
            echo "<li><a href='$files[$i]'>".heir($files[$i])."</a></li>";  
        }
    }
    if(count($files) == 0) {
        echo "Sorry, no hits.";
    }
?>


The accepted answer is really nice, but it made me think of the Spl iterators on the rocks. Fabien Potencier explains how he created the Finder classes in symfony here:

http://fabien.potencier.org/article/43/find-your-files

I also use his finder classes, they have a very nice chained interface.

  • 5.3+ standalone version (From sf2): https://github.com/symfony/Finder

Example:

use Symfony\Component\Finder\Finder;

$finder = new Finder();
$finder->files()->in(__DIR__);

foreach ($finder as $file) {
   print $file->getRealpath()."\n";
}

and also..

$finder->files()->name('*.php');
// or 
$finder->files()->size('>= 1K')->size('<= 2K');
$finder->date('since yesterday');

Documentation: http://symfony.com/doc/2.0/cookbook/tools/finder.html

The PHP5.2+ version from the sf1.4 framework: http://svn.symfony-project.com/branches/1.4/lib/util/sfFinder.class.php

This version is slightly different, and less fancy, but also does the job. You'll need to create an sfException class though, it's its only tie-in with the symfony framework. You may create your own sfException Class:

class sfException extends Exception { }

Documentation can be found here: http://www.symfony-project.org/cookbook/1_2/en/finder

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜