返回不带echo PHP的函数结果


Return function results without echo PHP

我正在尝试用集合文件夹结构中包含的内容自动生成的内容自动填充一组页面。

我发现了一些代码,这是我想要的大部分内容,但我正在尝试修改它,使其成为一个可以在不同文件夹中多次调用的函数。原始代码使用了一个简单的echo,而不是一个函数。

我已经编辑了它,并将其作为一个函数,但我一直在想如何让函数返回指定文件夹中的所有结果。

我正在使用这个功能:

    function getDirContents($dir) {
      date_default_timezone_set("Australia/Brisbane");
      $hide = ".";
      if (!isset($_SERVER['QUERY_STRING']) || $_SERVER['QUERY_STRING'] == "" || substr($_SERVER['QUERY_STRING'],0,2) == ".." || strstr($_SERVER['QUERY_STRING'], "..")) {
          $currdir = "$dir";
      } else {
          $currdir = urldecode($_SERVER['QUERY_STRING']);
      }
      if ($currdir == "$dir") {
          $label = "Root";
        } else {
          $path = explode('/', $currdir);
          $label = $path[count($path)-1]; 
      }
      // Opens directory
      $myDirectory = opendir($currdir);
      // Gets each entry
      while ($entryName = readdir($myDirectory)) {
          $dirArray[] = $entryName;
      }
      // Closes directory
      closedir($myDirectory);
      // Counts elements in array
      $indexCount = count($dirArray);
      // Sorts files
      //sort($dirArray);
      // Loops through the array of files
      for ($index = 0; $index < $indexCount; $index++) {
          // Decides if hidden files should be displayed, based on query above.
          if (substr("$dirArray[$index]", 0, 1) != $hide ) {
              // Resets Variables
              $favicon = "";
              $class = "file";
              // Gets File Names
              $name = $dirArray[$index];
              $namehref = ($currdir == "." ? "" : $currdir . '/') . $dirArray[$index];
              $fullname = $currdir . '/' . $dirArray[$index];
              // Gets Date Modified
              $modtime = date("M j Y g:i A", filemtime($fullname));
              $timekey = date("YmdHis", filemtime($fullname));
              // Separates directories, and performs operations on those directories
              if (is_dir($currdir . '/' . $dirArray[$index])) {
                  $extn = "&lt;Directory&gt;";
                  $size = "&lt;Directory&gt;";
                  $sizekey = "0";
                  $class = "dir";
                  // Gets favicon.ico, and displays it, only if it exists.
                  if (file_exists("$namehref/favicon.ico")) {
                      $favicon = " style='background-image:url($namehref/favicon.ico);'";
                      $extn = "&lt;Website&gt;";
                  }
                  // Cleans up . and .. directories
                  if($name=="."){$name=". (Current Directory)"; $extn="&lt;System Dir&gt;"; $favicon=" style='background-image:url($namehref/.favicon.ico);'";}
                  if($name==".."){$name=".. (Parent Directory)"; $extn="&lt;System Dir&gt;";}
                  if ($currdir == "." && $dirArray[$index] == "..")
                      $namehref = "";
                  elseif ($dirArray[$index] == "..") {
                      $dirs = explode('/', $currdir);
                      unset($dirs[count($dirs) - 1]);
                      $prevdir = implode('/', $dirs);
                      $namehref = '?' . $prevdir;
                  }
                  else
                      $namehref = '?' . $namehref;
              }
              // File-only operations
              else {
                  // Gets file extension
                  $extn = pathinfo($dirArray[$index], PATHINFO_EXTENSION);
                  // Prettifies file type
                  switch ($extn){
                      case "png": $extn="PNG Image"; break;
                      case "jpg": $extn="JPEG Image"; break;
                      case "jpeg": $extn="JPEG Image"; break;
                      case "svg": $extn="SVG Image"; break;
                      case "gif": $extn="GIF Image"; break;
                      case "ico": $extn="Windows Icon"; break;
                      case "txt": $extn="Text File"; break;
                      case "log": $extn="Log File"; break;
                      case "htm": $extn="HTML File"; break;
                      //case "html": $extn="HTML File"; break;
                      case "xhtml": $extn="HTML File"; break;
                      case "shtml": $extn="HTML File"; break;
                      case "php": $extn="PHP Script"; break;
                      case "js": $extn="Javascript File"; break;
                      case "css": $extn="Stylesheet"; break;
                      case "pdf": $extn="PDF Document"; break;
                      case "xls": $extn="Spreadsheet"; break;
                      case "xlsx": $extn="Spreadsheet"; break;
                      case "doc": $extn="Microsoft Word Document"; break;
                      case "docx": $extn="Microsoft Word Document"; break;
                      //case "zip": $extn="ZIP Archive"; break;
                      case "htaccess": $extn="Apache Config File"; break;
                      case "exe": $extn="Windows Executable"; break;
                      default: if($extn!=""){$extn=strtoupper($extn);} else{$extn="Unknown";} break;
                  }
                  // Gets and cleans up file size
                  $size = pretty_filesize($fullname);
                  $sizekey = filesize($fullname);
               }
              $row = "<td><a href='$namehref'>$name ($extn, $size)</a>    </td>";
              return $row;   
        }
      }
    }

然后用一个类似的变量调用它

 $Term1 = getDirContents('myFolder/Structure/');
 echo $term1;

这会打印出文件夹中的第一个文件,但我一辈子都不知道如何将其全部列出。

我的理解是,当你在函数中使用return时,它会停止函数,但它所在的for循环不应该重新运行吗?

我觉得我缺少一些基本的东西,所以非常感谢你的帮助。

一个非常简单的修复方法是更改如何分配表行字符串以将下一行附加到保持变量$row(使用.=),然后将return移动到循环之外。

我不会复制你的全部代码,但最后几行看起来是这样的;

          $row .= "<td><a href='$namehref'>$name ($extn, $size)</a>    </td>";
          // deleted 'return $row;' and moved to below
    }
  }
  return $row;
}

希望这能有所帮助。

PS:当我们将字符串附加到$row时,最好在函数顶部初始化它,以确保内存中没有$row的值。要做到这一点,只需编写$row = '';(即,将其设置为一个空白字符串)。