views:

52

answers:

1

How can i write an recursice directory searcher that takes a geven string and return the whole path plus the filename in php?

+3  A: 

The php function dir will help you.

http://php.net/manual/en/class.dir.php

There is an example in the notes of the docs (by sojka at online-forum dot net) that shows doing this, which I have included below...

<?php
public static function getTreeFolders($sRootPath = UPLOAD_PATH_PROJECT, $iDepth = 0) {
      $iDepth++;
      $aDirs = array();
      $oDir = dir($sRootPath);
      while(($sDir = $oDir->read()) !== false) {
        if($sDir != '.' && $sDir != '..' && is_dir($sRootPath.$sDir)) {
          $aDirs[$iDepth]['sName'][] = $sDir;
          $aDirs[$iDepth]['aSub'][]  = self::getTreeFolders($sRootPath.$sDir.'/',$iDepth);
        }
      }
      $oDir->close();
      return empty($aDirs) ? false : $aDirs;
}
?>

There are lots of other similar examples from other people on the same page, so find one that you like and go from there...

rikh
but that doesnt take an filename as args like getTreeFolders($filename,$path)
streetparade
There are lots of examples on the link I provided that perform lots of different recursive searches of a directory structure.
rikh