                                                                                
<?php
//-->-->
//--Description:
//-- This function creates an Html select element which allows the
//-- user to choose from the files and directories in a particular 
//-- directory on the web-server. The function returns a string containing
//-- HTML which when displayed in a web page creates the 'select'.
//-- 
//--
//--Parameters
//--  $sCurrentDirectory: This parameter is somewhat misnamed, it really
//--  should be called something like $sBaseDirectory or just $sDirectory
//--  it is the directory whose files and directories will be displayed. 
//-- 
//--  $sFormActionUrl: this is the URL which is the 'action' property
//--  for the form element which contains the HTML select element.
//--
//--Used By:
//--  The script notepad.php and webpad.php scripts use this function
//--  to allow the user to easily change to another file. There is a 
//--  piece of javascript which removes the need for the user to press
//--  the button to activate the form       
//--
  function fnSwitchFileSelect
    ($sCurrentDirectory, 
     $sFormActionUrl, 
     $bDisplayDirectories = false)
  {

    $sReturn = "
    <form  action = '$sFormActionUrl'  method = 'get'  
             name = 'switchFileForm'>
    <input   type = 'submit'  
            value = 'Switch to a Different File'>
    <select  name = 'file'
         onChange = 'document.forms.switchFileForm.submit()'>
    <option value=''>-Choose A File To Switch To-</option>";
    //$sCurrentDirectory = dirname($sFilePath);

      $aaFiles = '';
      $dh  = opendir($sCurrentDirectory);
      while (false !== ($sMemberFileName = readdir($dh)))
      {
        $sCurrentFilePath = "$sCurrentDirectory/$sMemberFileName";
        if (is_dir($sCurrentFilePath))
        {
           if ($sMemberFileName != '.')
           {
              $aaDirectories[] = $sMemberFileName;
           }
        }
        else
        {
           $aaFiles[] = $sMemberFileName;
        }
      } //-- while more files in the directory
       
      sort($aaFiles);
      sort($aaDirectories);
      reset($aaFiles);
      reset($aaDirectories);
      
      $sFileSeparator = '\\';
      foreach ($aaDirectories as $sMemberFileName)
      {
        if ($sMemberFileName == '..')
        {
          $sParentDirectory = preg_replace('#[^/]+$#', '', $sCurrentDirectory);
          //$sParentDirectory = basename($sCurrentDirectory);
          $sReturn = 
           $sReturn."<option  value = '".htmlspecialchars($sParentDirectory)."'>
          [Parent Directory]</option> ";
        }
        else
        {
           $sMemberFilePathEncoded = 
             htmlspecialchars("$sCurrentDirectory/$sMemberFileName");
           $sReturn = $sReturn."<option  value = '$sMemberFilePathEncoded'>
                 $sMemberFileName/</option> ";
        } //-- 
      } //-- foreach directory

      foreach ($aaFiles as $sMemberFileName)
      {
         $sMemberFilePathEncoded = 
             htmlspecialchars("$sCurrentDirectory/$sMemberFileName");
         $sReturn = $sReturn."<option  value = '$sMemberFilePathEncoded'>
            $sMemberFileName</option> ";
      } //-- foreach file

    $sReturn = $sReturn."
      </select>
      </form>";

     return $sReturn;

   } //-- function: fnSwitchFileSelect


//--<--<--<--<--<--<--<--<
?>

                                                                