For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > February 2007 > recursive search path class









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author recursive search path class
Jean Pierre Daviau

2007-02-16, 6:58 pm

Hi everyone,
I am trying to create a class that find a file in its subfolders.
See the results below


==================== classe import_class
============================
<?php
// php -check-f import_class.php
/*

class import_class {

var $safepath;
var $basepath;
var $classe;

function import_class($input){
if(strstr($input, ".php")){
$this->classe = $input;
$this->path = str_replace('\\', '/', __FILE__);
$fichier = substr($this->path, 0, strrpos($this->path, "/"));
$this->basepath = $fichier . "/";
$this->safepath = $this->basepath;
print "construct: $this->safepath\n";
}else{
$this->basepath = $input;
print "construct: $input\n";
}
}

function OpenAllDir()
{
$cpt = 0;

// On ouvre le dossier
$dossier = opendir($this->safepath);

// On parcourt le dossier
while($dir = readdir($dossier))
{

if(is_dir($dir) && $dir != '.' && $dir != '..')
{
$PathToRepertoire = str_replace($PathToRepertoire, '.', '' );
$this->safepath = $this->safepath . $PathToRepertoire . $dir .
"/";
//print "OpenAllDir: $this->safepath\n";

$instance = new import_class ($this->safepath);
$instance->OpenAllDir();
}

if ($dir === $this->classe) {
$this->basepath = $this->basepath;
print "Le fichier $dir existe\n";
break;
}
print "$dir\n";
}

closedir($dossier);
}

}//fin de class

$nw = new import_class("haha.php");
$nw->OpenAllDir();
================================= end class ==================
C:/PHP/commandLine/
| +Part_1
| +Part_2
| +node
| +file to find

construct: C:/PHP/commandLine/
..
...
cmdline.php
import_class.php
import_class_back.php
incl_class.php
info.php
construct: C:/PHP/commandLine/Part_1/

Warning: readdir(): supplied argument is not a valid Directory
resource in C:\PHP\commandLine\import_class.php on line 45
Warning: closedir(): supplied argument is not a valid Directory
resource in C:\PHP\commandLine\import_class.php on line 66
Part_1
Part_1.htm

construct: C:/PHP/commandLine/Part_1/Part_2/
Warning: readdir(): supplied argument is not a valid Directory
resource in C:\PHP\commandLine\import_class.php on line 45
Warning: closedir(): supplied argument is not a valid Directory
resource in C:\PHP\commandLine\import_class.php on line 66
Part_2
Part_2.htm
PHpLine.cmd



Thanks for your attention.

Jean Pierre Daviau
--
windows Xp
asus p4 s533/333/133
Intel(R) Celeron (R) CPU 2.00 GHz
Processor Radeon7000 0x5159 agp


Jean Pierre Daviau

2007-02-16, 6:58 pm

Better
==================== classe import_class
============================

class import_class {
var $basepath;
var $classe;
var $safepath;

function import_class($input){
if(strstr($input, ".php")){
$this->classe = $input;
$this->path = str_replace('\\', '/', getcwd());
$this->basepath = $this->path;
$this->safepath = $this->basepath;
print "constructorInput: $this->safepath \n";
}else{
$this->safepath = $this->basepath . $input;
print "constructorInput: $this->safepath\n";
}

}

function OpenAllDir()
{
$cpt = 0;

// On ouvre le dossier
$dossier = opendir($this->safepath);

// On parcourt le dossier
while($dir = readdir($dossier))
{
// .. le dossier au-dessus
if(is_dir($dir) && $dir != '.' && $dir != '..')
{
$PathToRepertoire = str_replace($PathToRepertoire, '.', '' );
$this->safepath = $this->basepath . $PathToRepertoire . "/" .
$dir;
//print "OpenAllDir: $this->safepath\n";

$instance = new import_class ($this->safepath);
$instance->OpenAllDir();
}

if ($dir === $this->classe) {
$this->basepath = $this->basepath;
print "Le fichier $dir existe\n";
break;
}
print "$dir\n";
}

closedir($dossier);
}

}//fin de classe

$nw = new import_class("haha.php");
$nw->OpenAllDir();
================================= end class ==================
| C:/PHP/commandLine/
| +Part_1 |
+node1
| +Part_2
| +node2
| +file to find

==============================

constructorInput: C:/PHP/commandLine
..
...
etc

constructorInput: C:/PHP/commandLine/Part_1
..
...
etc
node1
etc

constructorInput: C:/PHP/commandLine/Part_2
..
...
etc
node2
etc
======================
normal termination
===========


constructorInput: C:/PHP/commandLine/Part_1/node1
&
constructorInput: C:/PHP/commandLine/Part_1/node2

are not printed





Jean Pierre Daviau

2007-02-18, 7:57 am


> while($dir = readdir($dossier))
> {
> if(is_dir($dir) && $dir != '.' && $dir != '..')



> | C:/PHP/commandLine/
> | +Part_1 |
> +node1


> ==============================
>
> constructorInput: C:/PHP/commandLine
> .
> ..
> etc
>
> constructorInput: C:/PHP/commandLine/Part_1
> .
> ..
> etc
> node1
> etc
>
> constructorInput: C:/PHP/commandLine/Part_2
> .
> ..
> etc
> node2
> etc
> ======================
> normal termination
> ===========
>
>
> constructorInput: C:/PHP/commandLine/Part_1/node1
> &
> constructorInput: C:/PHP/commandLine/Part_1/node2
>
> are not printed


Why it does not open the +node1 dir but open the Part_1 dir ?


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com