I'm using DirectoryIterator to generate a linked-list of PDF files. (There's also some code to break up the file name to make the list more user friendly.) I'd like to sort the results by file name, but can't figure out how. I know I need to put the results into an array, and then sort the array. But, I can't find an example anywhere that does it quite like I am, so I can't figure out how to integrate the array/sort into my code, since my PHP is weak. Can anyone lend an assist?
($path is declared elsewhere on the page)
<?php
if (is_dir($path))
{
print '<ul>';
foreach (new DirectoryIterator($path) as $file)
{
if ($file->isDot()) continue;
$fileName = $file->getFilename();
$pieces = explode('.', $fileName);
$date = explode('-', $pieces[2]);
$filetypes = array(
"pdf",
"PDF"
);
$filetype = pathinfo($file, PATHINFO_EXTENSION);
if (in_array(strtolower($filetype), $filetypes))
{
print '<li><a href="' . $path . '' . $fileName . '">' . $pieces[2] . '</a></li>';
}
}
print '</ul>';
}
else
{
print $namePreferred . ' are not ready.</p>';
}
?>