                                            
<?php

//-- Description: 
//--   The idea of this function is to create an HTML <select> element
//--   (which is a "drop-down list" element) each of whose items in the
//--   list is a path to a directory on a web-server. Only those directories
//--   which are accessible from outside requests to the web-server should
//--   be displayed.
//--   
//--   
//-- Notes:
//--   
//-- See Also:
//--

function fnDirectoryTreeHtmlSelect
  ($sRootDirectory, $sElementName = 'directory')
{
  $aaTemp = '';
  $sResult = '';
  
  $sRootDirectory = trim($sRootDirectory);

  if (substr($sRootDirectory, -1) == '/')
  {
    $sRootDirectory = preg_replace('#/$#', '', $sRootDirectory);
  }

  $sRootDirectoryEncoded =
    htmlspecialchars($sRootDirectory);
  $sResult = "
    <select  name = '$sElementName'>";

  $sResult = $sResult."
    <option  value = '$sRootDirectoryEncoded'>
    $sRootDirectoryEncoded</option>";

  $sResult = $sResult.fnRecursivePathToHtmlOption($sRootDirectory);
  $sResult = $sResult."
     </select>";

  return $sResult;
} //-- function: fnDirectoryTreeHtmlSelect

function fnRecursivePathToHtmlOption($sDirectoryPath)
{
    //echo "trying to open $sDirectoryPath<br>";

    if ($dir = opendir($sDirectoryPath))
    {
      //$sDirectoryPathEncoded =
      //   htmlspecialchars($sDirectoryPath);

      while (false !== ($sFileName = readdir($dir)))
      {
        if ($sFileName == ".") continue;
        if ($sFileName == "..") continue;

        $sFilePath = $sDirectoryPath."/".$sFileName;
        $sFilePathEncoded = htmlspecialchars($sFilePath);

        if (is_dir($sFilePath))
        {
          $sReturn = $sReturn."
            <option  value = '$sFilePathEncoded'>
            $sFilePathEncoded</option>";

          $sReturn = 
            $sReturn.fnRecursivePathToHtmlOption($sFilePath);
        } //-- if 
      } //-- while more files in directory
      closedir($dir);
    }
    return $sReturn;

} //-- function fnRecursivePathToHtmlOption


?>
                                                    