PHP的readdir()返回“。 “和”..“条目

我正在为我的公司编写一个简单的networking报告系统。 我为index.php编写了一个脚本,它获取“reports”目录中的文件列表,并自动创build一个指向该报告的链接。 它工作正常,但我的问题是,readdir()不断返回。 除目录的内容外还有目录指针。 有什么办法来防止这个OTHER THAN循环返回的数组,并手动剥离它们?

下面是相关的好奇的代码:

//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); 

在上面的代码中,你可以在while循环中添加第一行:

 if ($report == '.' or $report == '..') continue; 
 array_diff(scandir($reportDir), array('.', '..')) 

甚至更好:

 foreach(glob($dir.'*.php') as $file) { # do your thing } 

不,这些文件属于一个目录,并且readdir应该返回它们。 我会考虑其他的行为被打破。

无论如何,只要跳过它们:

 while (false !== ($report = readdir($reportDir))) { if (($report == ".") || ($report == "..")) { continue; } ... } 

我不知道另一种方式,如“。” 和“..”也是适当的目录。 正如你正在循环,以形成正确的报告网址,你可能只是放在一点, if忽略...进一步处理。

编辑
Paul Lammertsma比我快一点。 这是你想要的解决方案;-)