php scandir ->

3 Cevap

Ben sizden önce ben şanslı olmadan arandı ..

Ben dosya / klasörler için arama yapabilirsiniz, hangi kendim için basit bir script arıyorum. Php sorularınızı (ben bu gerekiyor sanırım) Bu kod parçacığını buldu, ama bu benim için iş değil.

"Bir maske kullanarak bir dosya / dizin aramak için basit bir yol arayan oldu. İşte böyle bir fonksiyonudur.

Varsayılan olarak, bu işlev aynı dizin için bir çok zaman scaning önlemek için, bellekte scandir () sonucu devam edecektir. "

<?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); 
} 
?>

Herhangi bir yardım için teşekkür ederim,

Peter

3 Cevap

Sadece bir dosya aramak istiyorum ben, bu pasajı kullanabilirsiniz:

    <?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.";
    }
?>

Kabul cevabı gerçekten güzel, ama bana kayaların üzerinde Spl Yineleyicilerin düşündürdü. Fabien Potencier burada symfony Finder sınıfları nasıl yarattığını anlatıyor:

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

Ben de onun bulucu sınıfları kullanmak, onlar çok güzel bir zincirleme arayüzü var.

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');

Dokümantasyon: 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

Bu sürüm biraz farklı ve daha az fantezi değil, aynı zamanda iş yok. Eğer olsa bir sfException sınıf oluşturmak gerekir, bu onun sadece tie-symfony çerçeve ile. Kendi sfException Class oluşturabilir:

class sfException extends Exception { }

Belgeler burada bulunabilir: http://www.symfony-project.org/cookbook/1_2/en/finder