PHP readdir()返回"."和".."条目

DWi*_*ams 3 php linux readdir

我正在为我的公司编写一个简单的Web报告系统.我为index.php编写了一个脚本,该脚本获取"reports"目录中的文件列表,并自动创建指向该报告的链接.它工作正常,但我的问题是readdir()不断返回.和..目录指针以及目录的内容.是否有任何方法可以阻止此其他循环通过返回的数组并手动剥离它们?

以下是好奇的相关代码:

//Open the "reports" directory
$reportDir = opendir('reports');

//Loop through each file
while (false !== ($report = readdir($reportDir)))
{
  //Convert the filename to a proper title format
  $reportTitle = str_replace(array('_', '.php'), array(' ', ''), $report);
  $reportTitle = strtolower($reportTitle);
  $reportTitle = ucwords($reportTitle);

  //Output link
  echo "<a href=\"viewreport.php?" . $report . "\">$reportTitle</a><br />";
}

//Close the directory
closedir($reportDir);
Run Code Online (Sandbox Code Playgroud)

Pau*_*sma 16

在上面的代码中,您可以在while循环中作为第一行追加:

if ($report == '.' or $report == '..') continue;
Run Code Online (Sandbox Code Playgroud)