Bon, je balance un peu de code et quelques var_dump... defois que ça inspire...
Voila, j'essaie de mettre en place une classe de recherche de fichier ou de contenu de fichier.
voici comment je procède:
1 classe abstraite qui liste récursivement le répertoire visé:
<?php
abstract class RecursiveReadDir {
public $PathArray=array();
private $a=0;
public function PathList($dir) {
if (!is_dir($dir)) {
throw new MyException ($dir.' n\'est pas un repertoire valide');
}
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
$it->next();
while($it->valid()) {
$this->PathArray[$this->a]['Path']=$it->getPath();
$this->PathArray[$this->a]['FileName']=$it->getFilename();
$this->a++;
$it->next();
}
return $this->PathArray;
}
}
?>
var_dump de sortie (et donc ce que récupère la classe suivante):
array(7) {
[0]=>
array(2) {
["Path"]=>
string(10) "martien/js"
["FileName"]=>
string(5) "js.js"
}
[1]=>
array(2) {
["Path"]=>
string(12) "martien/test"
["FileName"]=>
string(9) "test.txt~"
}
[2]=>
array(2) {
["Path"]=>
string(12) "martien/test"
["FileName"]=>
string(8) "test.txt"
}
[3]=>
array(2) {
["Path"]=>
string(7) "martien"
["FileName"]=>
string(9) "index.php"
}
[4]=>
array(2) {
["Path"]=>
string(16) "martien/requests"
["FileName"]=>
string(11) "cherche.php"
}
[5]=>
array(2) {
["Path"]=>
string(11) "martien/cls"
["FileName"]=>
string(15) "styles_gepo.css"
}
[6]=>
array(2) {
["Path"]=>
string(11) "martien/cls"
["FileName"]=>
string(17) "buildform.cls.php"
}
}
viens ensuite la fameuse classe de recherche
<?php
class SearchStringOrName extends RecursiveReadDir {
public $FindArray=array();
public $FindResult=array();
public $FileNameSearch;
protected $NbFind=0;
public function __construct($Recherche,$TypeFind,$Dir) {
if ((empty($Recherche))||(empty($TypeFind))||(empty($Dir)))
throw new MyException ('paramètre manquant');
}
public function searchStrOrName($Recherche,$TypeFind,$Dir) {
$this->FindArray = parent::PathList($Dir);
$ar = new ArrayObject($this->FindArray);
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($ar));
while($it->valid()) {
if ($TypeFind==='FileName') {
echo $it->key() . ' => ' . $it->current() . ' => '.$it->getDepth ().'<br/>';
if ((strpos($it->current(),$Recherche))&&($it->key()==='FileName')) {
$this->FindResult[$this->NbFind][$it->key()]=$it->current();
$it->next();
$this->FindResult[$this->NbFind][$it->key()]=$it->current();
}
}
if ($TypeFind==='FileContent') {
$ContFile=file_get_contents(settype($it->current(),'string'));
if(ereg("$Recherche(.{0,40})",$ContFile,$sortie)) {
$this->FindResult[$this->NbFind][$it->key()]=$sortie[0];
$this->FindResult[$this->NbFind][$it->key()]=$it->current();
}
}
$this->NbFind++;
$it->next();
}
return($this->FindResult);
}
}
?>
mon soucis, c'est qu'au moment des comparaisons (je ne me concentre pour l'instant que des nom de fichier)
mon itérateur me renvoi
0 => Array
Path => martien/js
FileName => js.js
Path => martien/test
FileName => test.txt~
Path => martien/test
FileName => test.txt
Path => martien
FileName => index.php
Path => martien/requests
FileName => cherche.php
Path => martien/cls
FileName => styles_gepo.css
Path => martien/cls
FileName => buildform.cls.php
c'est très bien, mais je veux comparer les nom de fichier et conserver le PATH... j'espère que vous comprenez mon problème (le code me parait parlant)....
Merci de m'éclairer avant que je trouve une corde...
Linux - Ubuntu 8.04 - FireFox 3.0.7
~~(-_-)~~