+ Reply to Thread
Results 1 to 3 of 3

Thread: PHP find Directory

  1. #1
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    PHP find Directory

    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 by driveflexfuel; 10-26-2009 at 11:46 AM.

  2. #2
    ah-blabla's Avatar
    ah-blabla is offline x10 Lieutenant ah-blabla is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    375

    Re: PHP find Directory

    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 by ah-blabla; 10-26-2009 at 01:26 PM.

  3. #3
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: PHP find Directory

    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 Code:
    <?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 strlenMY_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>" ;

    ?>
    Nothing is always absolutely so.

+ Reply to Thread

Similar Threads

  1. Php directory help
    By thenewprogrammer in forum Programming Help
    Replies: 1
    Last Post: 10-25-2009, 12:27 AM
  2. CRON Jobs and PHP
    By deadimp in forum Tutorials
    Replies: 14
    Last Post: 11-27-2008, 05:09 PM
  3. Noticia De upgrade PHP espaņol
    By figu120 in forum General
    Replies: 2
    Last Post: 11-22-2007, 04:42 PM
  4. Important PHP Information
    By Bryon in forum News and Announcements
    Replies: 0
    Last Post: 11-21-2007, 02:08 PM
  5. "PHP Startup: Invalid Library" - Interesting error
    By javaguy78 in forum Free Hosting
    Replies: 5
    Last Post: 03-27-2007, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers