Php – Recursive directory searcer PHP

oopPHPrecursion

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

Best Answer

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...