我正在为我的公司编写一个简单的网络报告系统。我为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);发布于 2009-10-06 14:17:44
在上面的代码中,您可以将其作为while循环的第一行:
if ($report == '.' or $report == '..') continue;发布于 2009-10-06 14:17:20
array_diff(scandir($reportDir), array('.', '..'))或者更好:
foreach(glob($dir.'*.php') as $file) {
# do your thing
}发布于 2009-10-06 14:18:47
不,这些文件属于一个目录,因此readdir应该返回它们。我会认为所有其他行为都是被打破的。
不管怎样,跳过它们:
while (false !== ($report = readdir($reportDir)))
{
if (($report == ".") || ($report == ".."))
{
continue;
}
...
}https://stackoverflow.com/questions/1525850
复制相似问题