Recursively accessing the directories

admin

Administrator
Staff member
I am doing coding to arrange files in a folder. It reads the current working directory and fetches all file extensions and then arranges the files. Till here it is OK. But if there is any folder, it should be able to access that directory recursivley(Only after clicking on that link, prefered the result in another page).
Can anyone guide me?Post your existing code.Did you look at the DirectoryIterator in PHP5 SPL library? I use this and it works well for me.

<!-- m --><a class="postlink" href="http://us2.php.net/manual/en/ref.spl.phpPost">http://us2.php.net/manual/en/ref.spl.phpPost</a><!-- m --> your existing code.

I am new to PHP5. I am not sure about the structure and modularity of this code.
please help me out by pointing such mistakes and how to make the code more effectvie. Is it an effective way to create each object for each file type? Can u suggest an alternate way?
I am posting this small code. Please point out the mistakes..

<?
class RegexFilter extends FilterIterator
{
protected $regex;
public function __construct(Iterator $it, $regex)
{
parent::__construct($it);
$this->regex=$regex;
}
public function accept()
{
return preg_match($this->regex, $this->current());
}

}
class My_Filter
{
private $type;
public function __construct($type)
{
$this->type = $type;
}
public function Iterator()
{
$info = pathinfo($_SERVER['PHP_SELF']); //reading the current directory
$path=$info[dirname];
$dir = new DirectoryIterator($path); //creating the directory iterator
//$a = end(explode('.',$file4)); //fetches the file type
$filtered_dir = new RegexFilter($dir,'/'.$this->type.'/'); //creating the regexfilter class
foreach($filtered_dir as $file)
{
if(is_dir($file))
{
continue; // avoids the folders
}
print $file."<br>"; //printing the files
}
}
}
$my_filter = new My_Filter('php');
$my_filter->Iterator(); // prints all the php files
$my_filter = new My_Filter('html'); // prints the html files
$my_filter->Iterator();
?>
 
Back
Top