PHP readdir () returns records "." And ".."
I am coding a simple web reporting system for my company. I wrote a script for index.php that gets a list of files in the "reports" directory and automatically creates a link to that report. It works fine, but my problem here is readdir () keeps returning. and .. directory pointers in addition to directory contents. Is there a way to prevent this OTHER THAN loop through the returned array and delete them manually?
Here's the relevant code for the curious:
//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);
source to share
I don't know any other way like "." and ".." are the proper directories. Since you are looping about generating the correct report url anyway, you can just add a little if
that ignores .
and ..
for further processing.
CHANGE
Gender Lammertsma was a bit faster than me. This is the solution you want; -)
source to share