PHP readdir()返回“. “和”……“条目
发布时间:2020-12-13 22:19:33 所属栏目:PHP教程 来源:网络整理
导读:我正在为我的公司编写一个简单的Web报告系统.我为index.php编写了一个脚本,该脚本获
我正在为我的公司编写一个简单的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); 解决方法
在上面的代码中,您可以在while循环中作为第一行追加:
if ($report == '.' or $report == '..') continue; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |