PHP find Directory

driveflexfuel

New Member
Messages
159
Reaction score
0
Points
0
I have been looking for a solution to this function for the last 2 days and im going nuts lol. If anyone has any ideas please share them.

I need a function that takes a file name example('find_904972.php')searches directories starting at home directory to find that file and reports back the full directory the file is in

Code:
function find_directory($directory_name){

// search for directory



return $directory_location
}

example $direcotory_location = "uesr/public_html/directory/directory/"
 
Last edited:

ah-blabla

New Member
Messages
375
Reaction score
7
Points
0
Quick example:
$directory_name is the name of the directory where you want to start, INCLUDING a trailing /. $file_name is the name of the file. $list is the array in which you want to store the addresses of any files with the specified name (i.e. if you have multiple files with that name, all will appear in the list.)

Code:
function find_directory($directory_name, $file_name, &$list){
 $content = scandir($directory_name);
 foreach ($content as $value) {
  if ($value=="." || $value=="..") {
  } else {
   $absolute = $directory_name.$value;
   if ($value === $file_name) {
    $list[] = $absolute;
   } else if (@is_dir($absolute)) {
    find_directory($absolute."/", $file_name, $list);
   }
  }
 }
}
Horrible, but it works.

To use it:
Code:
$list = array();
find_directory("/home/wherever/","bla.bla.conf",$list);
Now you can do whatever you want with $list, which contains a list of full paths to all files with the name you wanted, in this case bla.bla.conf.
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
PHP 5 has classes to iterate over directories.

RecursiveDirectoryIterator class

You wrap that in a

RecursiveIteratorIterator


You can create a new class to extend

FilterIterator

The above links have some examples.

How to get it to do what you want...

PHP:
<?php
  
define("MY_ROOT" , "/home/USERNAME/public_html/" ) ;

## THE CUSTOM CLASS  EXTENDING  FilterIterator
## WHICH CONTAINS A RecursiveIteratorIterator
## WHICH ITERATES OVER A  RecursiveDirectoryIterator

class FilterIteratorIterator extends FilterIterator{   
    private $search_for ;
    public function __construct($path, $seek){
        parent::__construct( new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path) ) ) ; 
        $this->search_for = $seek ;
    }

    public function accept(){   
        $fn = $this->getInnerIterator()->getFilename();
        return  $this->search_for == $fn  ;
    }
}

### FUNCTION THAT USES THE CUSTOM ITERATOR

  function  get_files( $searching_for , $iter ){
    $len = strlen( MY_ROOT ) ;
    $target_len = $len +  strlen( $searching_for ) ;
    $all_files = array();
    foreach ( $iter as $fn=>$cur) {
        # SEVERAL WAYS TO STORE RESULTS....
        #$all_files[] = dirname( $fn ) . '/'; # abs path (not including filename) 
        #$all_files[] =  substr( $fn , $len ); # gives path relative to document root, with file name
        $all_files[] = $fn ; # Use this line instead if you want full absolute path
        #$all_files[] =  substr( $fn , $len , strlen( $fn )  - $target_len ); # gives path relative to document root,
    }

    return $all_files ;
  }

  $file_to_search_for = 'index.html' ; 
  $start_dir = MY_ROOT    ;
  $iter = new FilterIteratorIterator($start_dir ,  $file_to_search_for  );
  $the_files =  get_files( $file_to_search_for , $iter );
 
  
  echo "<pre>" ;
  var_dump( $the_files ) ;
  echo "</pre>" ;

?>
 
Top