getting all sub-directories

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    getting all sub-directories

    Hi,

    I'm using PHP 4.4.4 with Apache 2 on Fedora Core 6 Linux. Given a
    directory, how would I get all sub-directories? I found "scandir" but
    that only applies for PHP 5 and I think it returns both files and
    folders, whereas I only want the folders.

    Thanks for any help, - Dave
  • petersprc

    #2
    Re: getting all sub-directories

    This function will do it:

    function getFilesByType( $dir, $type = 'dir')
    {
    if (($dh = opendir($dir)) === false) {
    trigger_error(' opendir', E_USER_ERROR);
    } else {
    if ($dir[strlen($dir) -1] == DIRECTORY_SEPAR ATOR) {
    $sep = '';
    } else {
    $sep = DIRECTORY_SEPAR ATOR;
    }
    $list = array();
    while (($ent = readdir($dh)) !== false) {
    if ($ent == '.' || $ent == '..') continue;
    if (filetype("$dir $sep$ent") == $type) {
    $list[] = "$dir$sep$e nt";
    }
    }
    closedir($dh);
    return $list;
    }
    return false;
    }

    On Feb 10, 8:00 pm, "laredotorn...@ zipmail.com"
    <laredotorn...@ zipmail.comwrot e:
    Hi,
    >
    I'm using PHP 4.4.4 with Apache 2 on Fedora Core 6 Linux. Given a
    directory, how would I get all sub-directories? I found "scandir" but
    that only applies for PHP 5 and I think it returns both files and
    folders, whereas I only want the folders.
    >
    Thanks for any help, - Dave

    Comment

    • Michael Fesser

      #3
      Re: getting all sub-directories

      ..oO(laredotorn ado@zipmail.com )
      >I'm using PHP 4.4.4 with Apache 2 on Fedora Core 6 Linux.
      PHP 4 is dead, PHP 6 on the rise. It's time for an update.
      >Given a
      >directory, how would I get all sub-directories? I found "scandir" but
      >that only applies for PHP 5 and I think it returns both files and
      >folders, whereas I only want the folders.
      I suggest to update to PHP 5 and use the iterators from the SPL. Then
      with a simple 'foreach' loop you can easily loop through arbitrarily
      deep nested tree structures and search for whatever you like, e.g.

      $result = array();
      foreach (new DirectoryIterat or($yourPath) as $item) {
      if ($item->isDir() && !$item->isDot()) {
      $result[] = $item->getPathname( );
      }
      }

      Micha

      Comment

      Working...